SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF file_exists or an alternative?

    I have two websites hosted on the same server, MySite and MySite2. I have a script that displays images hosted on MySite on MySite IF those images exist. It also accepts any extension (.gif, .jpg, etc.) and detects the image's size. I want to modify that script to display images hosted on MySite on MySite2.

    So far, I've learned not to use URL's to check for images, because it apparently eats up bandwidth. However, someone on another thread gave me a really cool script that displays an image's URL minus "http://"...

    PHP Code:
    $server $_SERVER['HTTP_HOST'];
    $path '/images/GS/stamps/world/na/usa/';

    echo 
    $server $path 'az.jpg'
    It displays the following link, minus "http://", which is apparently automatically inserted on this forum:

    PHP Code:
    www.geobop.org/images/GS...d/na/usa/az.jpg 
    In the meantime, it occurred to me that I might not even need to use IF file_exists to begin with. My current file_exists script is set up to not return error messages if an image doesn't exist. So why not just use a script that displays an image (assuming it exists) and doesn't do anything if there is no image?

    In the meantime, the following script from the Manual (http://www.php.net/manual/en/functio...ists.php#68945) was recommended to me...

    PHP Code:
    <?php
    function url_exists($url)
    {
     
    $handle = @fopen($url"r");
     if (
    $handle === false)
      return 
    false;
     
    fclose($handle);
     return 
    true;
    }
    ?>
    But I don't have a clue how to implement it. I tried preceding it with $url = 'http://www.geobop.org/images/GS/stamps/world/na/usa/az.jpg'; (with and without the "http://"), but I couldn't see any results. But I don't know what "return" false and true mean. I stuck in an echo 'FALSE', but that didn't work, either.

    But maybe I should first go back to square one. There's an image at
    http://www.geobop.org/images/GS/stam.../na/usa/az.jpg, and I want to display it on www.geosymbols.org.

    But let's pretend I don't that image exists. All I know is that there MIGHT be an image at that address - with a .gif, .jpg or .png extension. Or perhaps there are two images there, one a .gif, the other a .jpg.

    What would be a good method to display that image if it exists?

    Thanks.

  2. #2
    SitePoint Addict pkSML's Avatar
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a hard time following exactly what you're looking for. I presume you're looking for a good way to determine if an image exists.
    If this is the case, I have some code for you that will do it.
    PHP Code:
    <PRE>
    <?php
    // syntax to use this script is checkURL.php?URL=http://www.yahoo.com

    // just specifies a default URL, if none is entered in the query string
    if ($_GET['URL']) {$URL $_GET['URL'];}
    else {
    $URL "http://stephen.calvarybucyrus.org";}

    //tries to open the URL
    $handle fopen("$URL""r");

    print 
    "\$URL = $URL\n\n"// prints the URL that is being requested

    if ($handle)  {print "URL exists";}
    if (!
    $handle) {print "URL does not exist";}

    fclose($handle);
    ?>
    </PRE>
    Last edited by stymiee; Sep 12, 2006 at 04:53.
    -Stephen

    Get a LitlURL to this page!

  3. #3
    SitePoint Addict pkSML's Avatar
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    In the meantime, the following script from the Manual (http://www.php.net/manual/en/functio...ists.php#68945) was recommended to me...

    PHP Code:
    <?php
    function url_exists($url)
    {
     
    $handle = @fopen($url"r");
     if (
    $handle === false)
      return 
    false;
     
    fclose($handle);
     return 
    true;
    }
    ?>
    But I don't have a clue how to implement it. I tried preceding it with $url = 'http://www.geobop.org/images/GS/stamps/world/na/usa/az.jpg'; (with and without the "http://"), but I couldn't see any results. But I don't know what "return" false and true mean. I stuck in an echo 'FALSE', but that didn't work, either.
    Here's my implementation of the code.

    PHP Code:
    <?php
    $checkThisUrl 
    "http://stephen.calvarybucyrus.org/index.php";

    function 
    url_exists($url)
    {
     
    $handle = @fopen($url"r");
     if (
    $handle === false)
      return 
    false;
     
    fclose($handle);
     return 
    true;
    }
    $query url_exists("$checkThisUrl");
    if (
    $query) {print "URL exists";}
    if (!
    $query) {print "URL does not exist";}
    ?>
    Last edited by stymiee; Sep 12, 2006 at 04:51.
    -Stephen

    Get a LitlURL to this page!

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Both of those work fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •