How do you rename a file in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

Again, the defines should really go somewhere early in the app's lifecycle so they're available to the entire application. Repetition is bad. Code reuse is good.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Code: Select all

define('DS', DIRECTORY_SEPARATOR);
$image_dir = dirname(__DIR__) . DS . 'images' . DS . 'productphotos' . DS;

define('IMAGE_DIR', $image_dir);
define('IMAGE_DIRSMALL', IMAGE_DIR . 'small' . DS);
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;

if (isset($photoprimaryname))
{
$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
$renameResult2 = rename(IMAGE_DIRSMALL . $photoprimaryname, IMAGE_DIRSMALL . $photoprimarynewname);

// Evaluate the value returned from the function if needed
if ($renameResult == true) {
    echo $photoprimaryname . " is now named " . $photoprimarynewname;
    mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
} else {
     echo "Could not rename that file";
}
}
[text]Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\): The system cannot find the file specified. (code: 2) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 218

Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\small\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\small\): Access is denied. (code: 5) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 219
Could not rename that file [/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

Indeed. When the new file name isn't specified, you're going to have a bad time. Notice the additional checks I added.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Ahhh yes that flagged up I had done something wrong further down in the <form> field.
And I've just used similar code for the Gallery images, though they use a field that is formed with | and $token, but that's renaming fine too. Thanks!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply