简体   繁体   中英

rename file from /tmp dir to another destination

I try to move a temporary file located in the /tmp dir to another dir somewhere else on the server using the rename() function. But I get an error:

Permission denied (Code: 2)

for the temporary file. How can I move a temporary file to another location? If I check that the file exists with file_exists() I get true. And if I copy() the temporary file it works fine.

Here's my code so far:

 $toPath = '/var/www/htdocs/myproject/some/file.pdf'

 $fileName = 'myfile.pdf';
 $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName;

 rename($filePath, $toPath); // Permission denied (Code: 2) here

this is caused by sticky bit set on the directory:

drwxrwxrwt. 8 root root 4096 Feb 6 09:38 .

sticky bit basically stops non-owners of file to rename or delete the file. It is usually set on /tmp and similar directories where multiple users have write permissions and/or save temporary files in, in order to prevent accidental deletion.

For more info, see: https://www.thegeekstuff.com/2013/02/sticky-bit/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM