How do you rename a file in PHP?
Moderator: General Moderators
Re: How do you rename a file in PHP?
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?
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";
}
}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.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
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?
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!!
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.
All the best from the United Kingdom.