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

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you rename a file in PHP?

Post 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";
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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 »

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 »

Syntax looks OK. Have you tried with the absolute path rather than the relative path?
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 »

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";
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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 »

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";
}
}
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 »

Does /images exist? Seems more likely it would be /home/simon/public_html/images or /var/www/yoursite/images or some such.
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 »

Oh I see what you mean.
Locally it is in c:/xampp/phpmyadmin/site/images.. and so on.
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 »

You probably want to use __DIR__, possibly combined with dirname.
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 »

Sorry I have just looked that up, but still none the wiser.
Never seen that before.
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 »

__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.
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 »

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";
}
}
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 »

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);
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 »

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";
}
}
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 »

You'll notice that your first argument is missing the file name.
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 »

I'd also recommend using DIRECTORY_SEPARATOR instead of / because Windows
Post Reply