Strange rename () behavior in PHP

I am having a strange problem with my php. Whenever I try to move a file using rename (), not only the file does not move, but the directory into which it must be copied is deleted along with all the files inside it. Source:

rename('temp.odt', 'tmp/report.odt'); 

but I already tried other path separators like

 rename('temp.odt', 'tmp\report.odt'); rename('temp.odt', 'tmp\\report.odt'); rename('temp.odt', 'tmp' . DIRECTORY_SEPARATOR . 'report.odt'); rename('C:\wamp\www\zaiko\temp.odt', 'C:\wamp\www\zaiko\tmp\report.odt'); 

all to no avail. The code comes from a third-party module that is used in the system I'm working on.

Points are well checked:

  • The temp.odt file exists in the current directory;
  • The directory 'tmp' exists and there are several files in it. Also it is not readable only.
  • The target file does not exist yet (the actual file name has a timestamp, I reduced it here for simplicity)

After rename () is run, the β€œtemp.odt” file remains untouched at its original location, and the β€œtmp” folder has disappeared , as well as everything inside it. The following warning is issued:

(!) Warning: rename (temp.odt, tmp \ report.odt) [function.rename]: the system could not find the specified path *. (code: 3) in C: \ wamp \ www \ zaiko \ modules \ mod_deliver.php on line 192

* Translated from Portuguese

Launch: Apache 2.2.17 with PHP 5.3.5 on Windows XP with NTFS


Editing:

Just found the cause of the problem. It turns out that the module used by the application uses, in turn, a compression library; this library uses a temporary folder with the same name as the application used by the application.

It should use some kind of cache, which explains why the error did not appear 100%.

The problem is resolved by changing the name of the "tmp" folder to something else.

Thank you all for your time and it is a pity to bother you with such a stupid thing that, as it turned out, had absolutely nothing to do with my initial assumption and, therefore, with the question formulated.

+6
source share
1 answer

The PHP.net example tells you what to do β€” use the ROOT PATH for the file β€” this can usually be obtained using $_SERVER['DOCUMENT_ROOT'] (but this only applies to the htdocs / public_html directory - you need to specify the rest) or manually enter the path (but try to avoid this).

 <?php rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt"); ?> 

When guessing, the following should work (assuming that this is your path) - it also checks that your file really exists, so you can rename it - you need to make sure tmp/ really exists in the first place, but you will get an error message if this is not true:

 <?php $root = getcwd().DIRECTORY_SEPARATOR; // Obtain the current working dir $srcpath = $root."temp.odt"; // The file you want to rename $destpath = $root."tmp/report.odt"; // Where you want to rename the file to // make sure file exists and its movable if(is_writable($srcpath)){ // if it exists, rename it rename($srcpath, $dstpath); echo "File was renamed!"; } else { echo "It seems that the specified file doesn't exist!"; } ?> 

You avoided characters using backslashes - always use slashes (I know this is a single quote, that's fine, but if you use a double quote, then you are wondering what went wrong)!

+4
source

Source: https://habr.com/ru/post/908316/


All Articles