dude
dude

Reputation: 4982

How to change a filename of a file which is in a folder in PHP

Can anyone tell how to change a filename of a file which is a located in a folder using PHP?

rename("http://localhost/DXXX/photos/".$photoNamepart, "http://localhost/DXXX/photos/".$phototmpNamepart);



Error:Message: rename() [function.rename]: http wrapper does not support renaming

Upvotes: 1

Views: 6119

Answers (6)

Shahid Ahmed
Shahid Ahmed

Reputation: 57

rename($_SERVER['DOCUMENT_ROOT'].'/dir1/abc.png', $_SERVER['DOCUMENT_ROOT'].'/dir2/abc.png')

Above code works for me on CI

Upvotes: 1

Niranjan
Niranjan

Reputation: 45

This will works properly copy(getcwd()."/tmp/tmp_file.txt", getcwd()."/tmp/my_file.txt");

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

rename("/tmp/tmp_file.txt", "/tmp/my_file.txt");

Upvotes: 2

skyronic
skyronic

Reputation: 1635

You're using rename wrong here. fopen, file_get_contents work fine with URLs - but that's more out of convenience rather than correctness.

For the code that you've written - you first need the absolute path of the file that you want to rename. It will work only on the local machine and on files that your webserver is configured to have write-access on.

Let's say your web server root is WEB_ROOT,

rename(WEB_ROOT."/DXXX/photos/".$photoNamePart, WEB_ROOT."/DXXX/photos/".$photoTempNamePart;

should do the trick.

Upvotes: 1

Krishna Deepak
Krishna Deepak

Reputation: 1765

shell_exec('mv former_filename new_filename');

you should be having appropriate permissions to do this

Upvotes: 0

Arkh
Arkh

Reputation: 8459

I guess the rename function could help.

Upvotes: 4

Related Questions