0

I have the following code:

rename ('/original_dir/file','/new_dir/file');

When the code is run, I get the following message:

Warning: rename(/original_dir/file,/new_dir/file): Operation not permitted

The file is however copied to /newdir, but is not removed from /original_dir

I am using rename to have it moved, not copied.

Both /original_dir and /new_dir have permissions set to 0777

This should work. Any ideas?

EastsideDev
  • 6,257
  • 9
  • 59
  • 116

2 Answers2

1

Have you tried a copy() and a unlink()? It may give you a better look at what goes wrong.

if( copy('/original_dir/file', '/new_dir/file') ) {
unlink('/original_dir/file');
}

source

Edits: if the script is being called from the command line, does it work with sudo? If it is being called from a webpage, does it work if you change the owner of the file to apache? Did you get the exact error code and check it in man 2 rename per this comment?

Community
  • 1
  • 1
Sam H.
  • 4,091
  • 3
  • 26
  • 34
1

Rename is actually moving file with another name. As you can see from error, that operation is not permitted because, even though you have permission to write to file, you cannot delete the file if you are not owner of it on many Linux distributions. That's why your web server process is copying file, and not able to delete the old one.