PHP file_exists but rename fails "No such file or directory..."?

WallabyKid picture WallabyKid · Jan 19, 2013 · Viewed 11.7k times · Source

Thinking creation of the file in question lags a bit behind the offending function, I tried running a while loop to buy a little time before calling rename.

$no_file = 1;   
while($no_file && $no_file < 300)
{   // generation of Worksheet.xls may lag behind function -- WAIT FOR IT
    if(file_exists($old_path))
    {   $no_file = 0;
        rename($old_path, $new_path);
    }   else $no_file++;
}
if($no_file) die("Error: Worksheet.xls not found");

In this configuration, I'm thinking rename() can only be called if file_exists() returns true, but for the life of me I can't figure out how / why then rename() gets called and then fails returning...

PHP Warning: rename(C:\wamp\www\demox/wp-content/plugins/cat-man/store-manager/summary/worksheets/Worksheet.xls,C:\wamp\www\demox/wp-content/plugins/cat-man/store-manager/summary/statements/TESTING/2012/Worksheet.xls) No such file or directory...

Answer

nickb picture nickb · Jan 19, 2013

It's probably telling you statements/TESTING/2012/ doesn't exist. Create those directories with mkdir() so it will be able to save the file.

mkdir( 'C:\wamp\www\demox/wp-content/plugins/cat-man/store-manager/summary/statements/TESTING/2012/', 777, true);