file_exists() function doesn't return a value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cooldht
    New Member
    • Jul 2007
    • 12

    file_exists() function doesn't return a value

    Hi all,

    I ran into a cool function in a google search, called file_exists that would do exactly what I need it to do. Unfortunately, I think something is wrong with the logic I'm using, because it returns a value of null instead of true or false. It seems to work great if I check for something in the base directory, but when I try something outside of the root, it fails. The check file code looks like this:
    [code=php]if (file_exists($f ilename)) {
    echo "The file $filename exists";
    } else {
    echo "The file $filename does not exist";
    }[/code]
    If I say $filename = 'abcdef.php' which exists in the root directory, it returns true and everything is fine. But, if $filename = '/dir1/dir2/abcdef.php', it doesn't return true or false.

    Does anyone have any ideas if I am using the correct structure for specifying the directories?

    Thanks!
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, cooldht.

    Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

    What OS is your server running?

    Try checking for errors.

    Comment

    • cooldht
      New Member
      • Jul 2007
      • 12

      #3
      Hi PB,

      Sorry about leaving out the tags. I'm assuming you wanted to know the PHP version on the server. That is 4.3.8-1.1. Just in case, the MYSQL version is 3.23.58-4.

      Thanks!

      cooldht

      Comment

      • cooldht
        New Member
        • Jul 2007
        • 12

        #4
        Sorry again,

        I forgot to mention that I have the debugging on in the page, and didn't receive any error messages.

        Thanks,

        ct

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, cooldht.

          *n?x or Windows?

          Comment

          • cooldht
            New Member
            • Jul 2007
            • 12

            #6
            I'm not the main person that deals with the server side of things, so forgive me if I don't answer correctly. I can access the server, so I can view certain statistics. I can see the OS running says Linux 2.6.8-022stab070.9-enterprise. Is that what you are looking for?

            Thanks,

            ct

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, CT.

              That'll do it. Thanks.

              Can you verify that the file exists? For example:

              [code=php]
              var_dump(file_e xists($filename ));
              echo '<br />';
              fread($filename );
              [/code]

              If you see something like this:
              Code:
              NULL
              This is a sample file.
              Then you know that file_exists() is misbehaving because the file definitely exists.

              Comment

              • cooldht
                New Member
                • Jul 2007
                • 12

                #8
                Hi PB,

                I received the following when trying the code mentioned:

                bool(false)

                Warning: Wrong parameter count for fread() in /home/httpd/vhosts/httpdocs/pictures.php on line 59


                The funny thing is, if I put the page in the same directory as where the file exists, it finds the file ok by making $filename equal to roses.gif only, instead of specifying /images/roses.gif.

                Thanks,

                ct

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, CT.

                  One more:
                  [code=php]
                  var_dump($filen ame);
                  echo '<br />';

                  var_dump(file_e xists($filename ));
                  echo '<br />';
                  fread($filename );
                  [/code]

                  The 'wrong parameter count' error generally means that the value of the first parameter to fread() is null, which would lead me to wonder if perhaps $filename is not getting set properly.

                  Comment

                  • cooldht
                    New Member
                    • Jul 2007
                    • 12

                    #10
                    Thanks for the reply.

                    Ok, this is what was returned this time around....

                    string(17) "/images/roses.gif"
                    bool(false)

                    Warning: Wrong parameter count for fread() in /home/httpd/vhosts/httpdocs/pictures.php on line 61

                    Any ideas?

                    Thanks!

                    CT

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, CT.

                      My bad. I meant readfile().

                      At any rate, file_exists() is returning false because it's looking for a directory named 'images' at the root level of your server's hard drive.

                      Try this instead:
                      [code=php]
                      var_dump(file_e xists($_SERVER['DOCUMENT_ROOT'] . '/' . $filename));
                      [/code]

                      Comment

                      • jx2
                        New Member
                        • Feb 2007
                        • 228

                        #12
                        you can try "dot" for current dir or double dot for "parent directory
                        if your root contain two directories :

                        brother
                        sister

                        and your script is in sister
                        e.g. "./sister/sisterscript.ph p"

                        to acces brother you need:

                        e.g. "../brother/brotherscript.p hp"

                        regards
                        jx2

                        Comment

                        • cooldht
                          New Member
                          • Jul 2007
                          • 12

                          #13
                          Hi guys,

                          I selectively forgot to mention that I had tried the old ../ trick right off the bat, and didn't seem to do it. However, the DOCUMENT_ROOT trick seems to work ok. Thanks PB! I think I can make due doing it this way. Although, I'm a little surprised this worked, but the ../ trick didn't. Oh well, live and learn! Thanks for all the help!

                          CT

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Heya, CT.

                            Using '../' is a bit unpredictable because you don't necessarily know the current working directory.

                            Fortunately, PHP has a function for that!

                            Comment

                            • cooldht
                              New Member
                              • Jul 2007
                              • 12

                              #15
                              Thanks again PB. That could be very awesome. If that function works right for me, I could very well find myself doing some re-design on various websites as I think it could be a useful little gadget!

                              Thanks,

                              CT

                              Comment

                              Working...