Why does this rename script only half work?

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

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

Why does this rename script only half work?

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) && !empty($photoprimaryname) && isset($photoprimarynewname) && !empty($photoprimarynewname))
{
$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";
}
}
I'm using this script to run renaming tools, so files with spacing can be renamed without the space.

It works on the $renameResult", but on the one with the "small" subfolder, it fails.
It renames it in the database, and the top level image, but it just won't do the small one.

Yet, it works on another site with the same script.
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: Why does this rename script only half work?

Post by Celauran »

What do you mean "it fails"? What errors are reported? Have you checked that the files you're attempting to rename actually exist?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this rename script only half work?

Post by simonmlewis »

Sorry I should have given more detail.
The files do exist, as it renames the file in /productphotos/.
It updates the entry in the database.
The file does exist in /productphotos/small/ because on the pages where there are the small thumbnails, they do show.

No errors that I can see on screen. It just renames the main file and the db, and where there was a thumbnail, I Get the cross showing.
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: Why does this rename script only half work?

Post by Celauran »

Check the log. Check the return value of the rename operation. Check the values of both rename arguments. Use file_exists.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this rename script only half work?

Post by simonmlewis »

There is no log that I can find as it is local.
How do I check the return value? $renameResult2 always shows a 1, but then so does the other one!

The file 100% exists.
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: Why does this rename script only half work?

Post by Celauran »

There's a log. I can't tell you where it is -- it's your system -- but there's a PHP log. Check Apache and PHP configs.

Is the target directory writable?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this rename script only half work?

Post by simonmlewis »

Sometimes I really wonder about my head.
It DOES work. The consumer page was looking for the images on the LIVE server!!! What a fool.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply