Page 1 of 3
How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 10:13 am
by simonmlewis
This is where I am up to. Failing!
It will update the DB of course, but it's the filename itself that isn't renaming.
I'm probably going wrong in the 'source' but.
A bit of guidance please. thanks.
Code: Select all
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
$photoprimarynamesource = "../$photoprimaryname";
$photoprimarynewnamesource = "../$photoprimarynewname";
mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
$renameResult = rename($photoprimarynamesource, $photoprimarynewnamesource);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
echo "Could not rename that file";
}
}
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 10:25 am
by simonmlewis
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 10:32 am
by Celauran
Syntax looks OK. Have you tried with the absolute path rather than the relative path?
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 10:35 am
by simonmlewis
I tried this, thinking it would work, but again, doesn't touch the image itself.
But yes, have put in the local full URL in too, doesn't do anything.
I'm getting this error now:
[text]Warning: rename(): http wrapper does not support renaming in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 218
Could not rename that file [/text]
Code: Select all
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
$photoprimarynamesource = "images/productphotos/".$photoprimaryname;
$photoprimarynewnamesource = "images/productphotos/".$photoprimarynewname;
mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
$renameResult = rename("/images/productphotos/$photoprimarynamesource", "/images/productphotos/$photoprimarynewnamesource");
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
echo "Could not rename that file";
}
}
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 10:46 am
by simonmlewis
I spotted an error in the 'source' it quoted at the top, but it still has that code 3 error.
Code: Select all
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
$renameResult = rename("/images/productphotos/$photoprimaryname", "/images/productphotos/$photoprimarynewname");
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
echo "Could not rename that file";
}
}
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 11:19 am
by Celauran
Does /images exist? Seems more likely it would be /home/simon/public_html/images or /var/www/yoursite/images or some such.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 11:25 am
by simonmlewis
Oh I see what you mean.
Locally it is in c:/xampp/phpmyadmin/site/images.. and so on.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:03 pm
by Celauran
You probably want to use
__DIR__, possibly combined with
dirname.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:07 pm
by simonmlewis
Sorry I have just looked that up, but still none the wiser.
Never seen that before.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:13 pm
by Celauran
__DIR__ returns the full path to the current directory. dirname returns the containing directory. So dirname(__DIR__) will return the full path to the current director's parent. Probably easiest to use this to set some constants in your bootstrapping.
Code: Select all
$image_dir = dirname(__DIR__) . '/images/';
define('IMAGE_DIR', $image_dir);
or some such.
That way you can just reference IMAGE_DIR throughout your codebase rather than ../images ../../images and the like.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:20 pm
by simonmlewis
But then how do I use $image_dir in the rename() part of the code?
Code: Select all
$renameResult = rename($image_dir."$photoprimaryname", $image_dir."$photoprimarynewname");
??
Code: Select all
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
$image_dir = dirname(__DIR__) . '/images/productphotos/';
define('IMAGE_DIR', $image_dir);
mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
$renameResult = rename("/images/productphotos/$photoprimaryname", "/images/productphotos/$photoprimarynewname");
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
echo "Could not rename that file";
}
}
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:28 pm
by Celauran
You're going to encounter problems defining constants that way as you cannot redefine a constant that has already been defined. This is why I specifically suggested putting it in some bootstrapping code. That aside, you've mapped the target directory to a constant, so just reference that constant in your rename code.
Code: Select all
$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:35 pm
by simonmlewis
This still fails.
[text]Warning: rename(C:\xampp\phpmyadmin\site/images/productphotos/,C:\xampp\phpmyadmin\site/images/productphotos/7461658.jpg): The process cannot access the file because it is being used by another process. (code: 32) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 218
Could not rename that file [/text]
Line 218 is:
[text]$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);[/text]
Code: Select all
$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
$image_dir = dirname(__DIR__) . '/images/productphotos/';
define('IMAGE_DIR', $image_dir);
mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
echo "Could not rename that file";
}
}
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:37 pm
by Celauran
You'll notice that your first argument is missing the file name.
Re: How do you rename a file in PHP?
Posted: Fri Jul 10, 2015 12:37 pm
by Celauran
I'd also recommend using DIRECTORY_SEPARATOR instead of / because Windows