Troubles wih file_exists()

Discussion in 'PHP' started by vmlive, Apr 3, 2010.

  1. #1
    Hello all,

    I have an image located at http://my_ip/~website/image.jpg

    A script for image resizing uses the function file_exists() to verify the image location in the following way:

    if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/~website/image.jpg"))
    {
    	echo 'Error: image does not exist';
    }
    Code (markup):
    The script returns the error message although the image exists and shows up when browsing to its http:// address.

    My only guess is that the symbol "~" causes the problem because the function works just fine on a different server with different url.

    Any ideas on how to make it work?
    Thanks in advance.
     
    vmlive, Apr 3, 2010 IP
  2. fwbbetsy

    fwbbetsy Peon

    Messages:
    235
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The reason your having the problem is because your calling out the who URL up to the file your currently using. Since your script will be pulling images from 2 directories up and not from the base directory the following code should work:

    
    <?php
    
    // Check To See If The File Does Not Exist
    if (!file_exists("./~website/image.jpg")){
    	echo 'Error: image does not exist';
    }
    ?>
    
    Code (markup):
    However should you decide later to allow the user to change the URL in a settings file it could look like this:

    
    <?php
    
    // This goes in your settings or config file...
    $path ="./folder/where/you/will/store/the/images/";
    
    // Include your config file
    include ('config.php');
    
    // Check To See If The File Does Not Exist
    if (!file_exists($path . "image.jpg")){
    	echo 'Error: image does not exist';
    }
    ?>
    
    Code (markup):
     
    fwbbetsy, Apr 4, 2010 IP
  3. vmlive

    vmlive Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply fwbbetsy.

    I have solved the problem by using a different path. It turns out that $_SERVER['DOCUMENT_ROOT'] does not display the actual path on the server which is, in fact, different. Moreover, "/~website" is not an existing directory, but a temporary address that redirects to "/home/website/public_html"
     
    vmlive, Apr 4, 2010 IP
  4. fwbbetsy

    fwbbetsy Peon

    Messages:
    235
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah ok, well I didn't have access to your server, but I was on the right track :)
     
    fwbbetsy, Apr 4, 2010 IP