0

Hi everyone. It's been a while since I've needed help with a PHP problem. Hopefully someone here knows what I'm doing wrong because I can't see it.

I am trying to use rename() to rename an image file which is in a subdirectory of a subdirectory the script is running in. The script is running in "/folder" and the images are held in "/folder/images/general"

The script I currently have is:

$old = "./images/general/item2.jpg";
$new = "./images/general/item44.jpg";

if(file_exists($old))
{
    rename($old, $new);
    echo "yes";
}
else
{
    echo "no";
}

The if statement is finding the file as my browser is outputting "yes" however the file is not being renamed. The permissions of the image file are 644 which as far as I am aware, is all it needs to be but I have tried with the permissions set to 666 and even 777. Have I missed something increadibly obvious?

I need the script to be able to rename the image as it's uploaded (was going to have it uploaded, then renamed unless there's a more efficient way?) and to rename files which already exist.

Unfortunately, error logging is disabled on the server this is hosted on and I don't have the necessary access to enable it. Is there maybe something I can put in die() to output the PHP error to the browser just like die(mysql_error()); would output the MySQL error?

5
Contributors
9
Replies
34
Views
4 Years
Discussion Span
Last Post by Borzoi
0

Check the picture properties, it may be read-only.

Then you may change the code to:

    $old = "./images/general/item2.jpg";
    $new = "./images/general/item44.jpg";
    if(file_exists($old))
    {
        rename($old, $new) or die('Error renaming file.');
        echo "The file has been renamed successfully.";
    }
    else
    {
        echo 'The file does not exist.';
    }
0

Thanks for the suggestion but it's not in ready only.

Since posting, I tried using copy() and unlink() but they also don't affect the file. When I put a die() message then the error is displayed but without the PHP error log, I don't know why it's failing. I'm suspecting that a configuration on the server may be preventing it because the script is working on my own server without problem. The PHP versions are different but are both PHP 5.X.

I have an alternative in place at the moment but it requires more work for the user.

0

Is the server on Linux? I tested the code on the server that runs on Linux and I had the same problem and after runing in Terminal 'chmod 777 -R mytestsite' it worked fine. I just gave all privileges to the folder that contains the php script file and the image.

0

If error logging is disabled, is it possible your host has disabled your ability to rename?

1

You could try ini_set to override the error handling (but that could be disabled too).

Votes + Comments
A function I forgot about which helps a lot.
0

I had completely forgot about the ini_set function. Just tried it and it's not disabled so that's helped a huge amount.

The problem was actually a mis-spelling in the path name. Simple error but I completely missed it, looking for a more complicated one.

Thanks for all your help.

0

I'm a little puzzled. If you mis-spelt the path name how did your browser output "yes"?

0

I think it is your code editing software problem or your specify folder is running at this moment. Close the folder and try again.

0

@paulkd: It was the path specified in the $new variable that was wrong. Specifically a folder name. The if statement only checked if the $old variable had a valid file so it outputted "yes" because the original file existed. After being able to view the error stating that the "file or folder does not exist" I spotted the error instantly. The code I put in my original post wasn't copy-pasted. I had written it out instead.

@igbal51: I use Notepad++ to code for syntax highlighting only. I don't use a WYSIWYG or any other code editor which adds loads of useless and non-compliant code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.