1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem with php file_exists() function

Discussion in 'PHP' started by offthedome, Nov 5, 2007.

  1. #1
    OK this has to be a noob question but it's got me stumped. I'm working with the file_exists() function. (http://w3schools.com/php/func_filesystem_file_exists.asp)

    Here's my issue:

    INPUT: echo file_exists("test.txt");

    1. test.txt does not exist:
    OUTPUT: (nothing)

    2. test.txt exists:
    OUTPUT: 1

    So this is why this is a noob question: My issue is, I need to test if a file exists that's not in the same directory as the script containing the function file_exists(). So I've tried

    3. echo file_exists("http://www.example.com/test.txt")
    OUTPUT: (nothing)

    4. echo file_exists("/test.txt")
    OUTPUT: (nothing)

    How do I check if test.txt exists if it's in a different directory than the script containing the function?
     
    offthedome, Nov 5, 2007 IP
  2. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    file_exists() just doesn't work with HTTP addresses. It only supports filesystem paths (and FTP, if you're using PHP5.)
     
    brendandonhue, Nov 5, 2007 IP
  3. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #3
    I've tried "/test.php", and it doesn't work.
     
    offthedome, Nov 5, 2007 IP
  4. iteamweb

    iteamweb Active Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    file_exist can be checked only using directories.

    if u have www folder and a subfolder called uploads ..
    u want to check a file 1.jpg exist in that folder

    u have ur file in www then u want to write the code as...

    if(file_exists("uploads/1.jpg"))
    echo "file is present";
    else
    echo "false";

    u can also use glob function for this purpose.
    first take all the files present in a folder and then check with the name...
     
    iteamweb, Nov 5, 2007 IP
  5. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That would be a file called "test.php" in the root directory of the system, is that what you wanted? It's likely that your PHP script doesn't have the proper permissions to access that directory.
     
    brendandonhue, Nov 5, 2007 IP
  6. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #6
    I need to run this test with multiple directories, since both files to be checked for existence and also the scripts checking for exisitence will be in various directories and also the scripts checking for the files will be in various directories. So counting back and saying something like "../../../../folder/test.php" is not an option.
     
    offthedome, Nov 5, 2007 IP
  7. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #7
    Problem seems to be solved. Here's what worked:

    
    if  (file_exists($_SERVER['DOCUMENT_ROOT']."/folder/test.txt") echo "file exists";
    
    PHP:
     
    offthedome, Nov 5, 2007 IP