webERP Forum

Full Version: Z_ChangeStockCode.php does not rename image file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Z_ChangeStockCode.php script does not rename the image file.

In a shared hosted installation (Hostgator), the rename function fails to rename but returns true :-(

        echo '<br />' . _('Changing any image files');
        if (file_exists($_SESSION['part_pics_dir'] . '/' .$_POST['OldStockID'].'.jpg')) {
            if (rename($_SESSION['part_pics_dir'] . '/' .$_POST['OldStockID'].'.jpg',
                $_SESSION['part_pics_dir'] . '/' .$_POST['NewStockID'].'.jpg')) {
                    echo ' ... ' . _('completed');
                } else {
                    echo ' ... ' . _('failed');

I already checked that the files exists and it is not renamed, but the echo said "completed", so rename function returned true.

Any clue why?

Tim replied by email:

I am going to guess that it is not the rename that returned completed,
but that actually it is the file_exists() that is returning false, as
this returns the same completed message. Can you try changing the
message for when the file_exists() fails, and so it will help narrow
down the problem.

Is this a windows machine?

Thinking about this further, I suspect the hoster is running php <=5.3
and is running it in safe mode (safe mode is removed in >5.3). This
file_exists() check is unnecessary really as obviously the rename will
just have no affect if the file doesn't exist. I suggest you try
removing that check and seeing if it works then.

Thanks Tim:

Already checked it out, and the file_exists() is working fine (as it does all over webERP when looking to read the image file, as in stocks.php), I replaced the echo text and the problem relies on the rename() function, I guess.

It is a windows client machine but weberp lives in a unix server machine, so interpreted in a unix environment.

PHP version 5.2.17
MySQL version 5.5.23-55

Could it be a permissions problem? 0644 is ok?
I've simplified that logic to remove the test for the file exists as suggested by Tim.
The file and part pics directory need to be writable by the web-server user www-data or nobody or whatever you unix distro uses for the web-server user
if the file/directory are owned by the web-server user then :

rwx r-x r-x

should do it ok.
Thanks Phil: Yes, it was permissions problem.

Anyway, I do believe we still need the file_exists() check.

many thanks!
Well won't the rename simply fail if the file does not exist?
Maybe I'd better put the test for file_exists() back if not!!

The file_exists() does fail depending on the user running the script.

On the very same machine against the very same webERP installation:
- If a regular user (not admin) runs Z_ChangeStockCode.php it says "No image to rename".
- logout and login as admin user... it works perfectly. It finds the image and renames it.
- logut and login again an a 3rd user (not admin) and fails again.

To test the issue added these 4 lines just before the if file_exists() to snif the filesnames and in both cases are OK.

        $oldfilename = $_SESSION['part_pics_dir'] . '/' .$_POST['OldStockID'].'.jpg';
        $newfilename = $_SESSION['part_pics_dir'] . '/' .$_POST['NewStockID'].'.jpg';
        echo ' OLDNAME ' .$oldfilename;
        echo ' NEW ' .$newfilename;

So, it has to do with some privileges needed for file_exists().

Could you pls test it?
This imaging SDK enables you to save image with customized file name and file path. You can also process your image with this SDK according to own requirements. You may have a try.