Results 1 to 6 of 6

Thread: [RESOLVED] Something weird going on with is_file...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Something weird going on with is_file...

    For an assignment we have to create a shopping cart. For some reason, this happens:

    Here's the code:
    PHP Code:
      $file_path=dirname(__FILE__) . "../images/products/" $_GET['picpath']; // It's just the file name here, such as foo.gif, or bar.jpg

      
    if (!is_file($file_path))
      {
        
    printf("Something went wrong (The file doesn't exist or is not writable: <b>" $file_path "</b>). Please remove it manually.");
      }

      if (
    is_file($file_path)) //file_exists() also works.
      
    {
        
    unlink($file_path);
      } 
    The result of this is strange. It prints that "something went wrong", then it removes the file any way? What's going on here? I have tried the same code with and without dirname(__FILE__), same result. I don't understand how it an IF can be TRUE and FALSE. Is it one of those things where you need like === or !== or something?

    You can be sure that this is the only code that has anything to do with testing for files etc.
    Last edited by Slyke; May 20th, 2009 at 05:46 AM.

  2. #2
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: Something weird going on with is_file...

    Well first, lets see wat is_file is returning, run this code before your two if statements:

    PHP Code:
    var_dump(is_file($file_path)) . "\n"
    and post the results back here. Does the file actually exist? Can you locate it using FTP?


    ILMV

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Something weird going on with is_file...

    Yep, the file exists. I'm running a WAMP server on Windows (Have tried it with Anaska's WAMP Server and Uniform Server, both latest versions) and the LAMP server that comes with Ubuntu, all same results. So I can be sure that the file exists.

    PHP Code:
    var_dump(is_file($file_path)) . "\n"
    The output for this is:
    bool(true)

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Something weird going on with is_file...

    just to test, you could try:

    PHP Code:
    if(is_file($filepath) === false){
      echo 
    'something is wrong.';
    }

    if(
    is_file($filepath) === true){
      
    unlink($filepath);

    however, I'm still not sure why is_file() would be behaving this way. is there any reason you can't just use file_exists(), or build an ELSE instead of using two IFs? but, even if you did, it still doesn't explain the strange behaviour!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Something weird going on with is_file...

    Yep, I'll try it with the 3 equals.

    The file_exists() function does the same thing.

    I have also tried it with an ELSE. It does the same thing:

    PHP Code:
      $file_path=dirname(__FILE__) . "../images/products/" $_GET['picpath']; // It's just the file name here, such as foo.gif, or bar.jpg

      
    if (!is_file($file_path))
      {
        
    printf("Something went wrong (The file doesn't exist or is not writable: <b>" $file_path "</b>). Please remove it manually.");
      }
      else
      {
        
    unlink($file_path);
      } 
    States the error, then removes the file.

    I know it's removing the file, because I'd check before running my code if the file is there, and it is. Then after running the code, the file is gone, but the error is still printed to screen.

    Edit:
    Yep, the tripple equals (is_file($filename)===true) works!

    Thanks! .
    Last edited by Slyke; May 20th, 2009 at 04:32 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Something weird going on with is_file...

    well, even though you've now fixed it -- I think you were doing something wrong. there is no possible way for you to set up an IF ELSE statement and have the script do both things. I'd make sure you were loading the right file, have it saved, not cached, etc. I've had many situations where I had an old version of a file running and was wondering why my changes weren't taking place. PHP interprets it as: if this condition is true, do this, and skip the rest. if this condition is false, skip to the next condition check (in this case, the ELSE). it wouldn't ever be processing both pieces of code. so, you were either having some bizarre error, or you must have been leaving something out!

    anyway, glad it works now!

Posting Permissions

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



Click Here to Expand Forum to Full Width