fcunited
fcunited

Reputation: 180

Can't rename folder (PHP + Linux)

I have next weird problem:

I create subfolder in Linux tmp folder with the help of PHP

mkdir(sys_get_temp_dir().DIRECTORY_SEPARATOR.'subfolder');

Then I try to move this folder with help of PHP rename() func. I try to do it like this:

rename('/tmp/subfolder', '/other/folder/name');

But it returns me weird warning:

Warning: rename(): The first argument to copy() function cannot be a directory

Is it something to do with access rights for these folders? Any ideas?

Upvotes: 0

Views: 1164

Answers (1)

J-P
J-P

Reputation: 1086

Is your /tmp on a different filesystem from your /other? This would be good practice if you wanted to separate your temporary file storage, which could fill up with rubbish, from e.g. /var (which could fill up with logs!)

If that's the case, then PHP has a core bug that prevents rename() from working reliably across filesystems:

https://bugs.php.net/bug.php?id=54097

The workaround quoted there is to use exec("mv "...) but more robustly you can do:

if (@rename($from, $to)) {
    return;
}
exec("mv " . escapeshellarg($from) . " " . escapeshellarg($to));

If you encapsulate this in a function or method then you can call it in place of bare rename() whenever you need it.

Upvotes: 4

Related Questions