Re: How do you rename a file in PHP?
Posted: Tue Jul 14, 2015 7:43 am
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.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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";
}
}