webERP Forum
Z_ChangeStockCode.php does not rename image file - Printable Version

+- webERP Forum (http://www.weberp.org/forum)
+-- Forum: webERP Discussion (/forumdisplay.php?fid=1)
+--- Forum: Problems / Bugs? (/forumdisplay.php?fid=8)
+--- Thread: Z_ChangeStockCode.php does not rename image file (/showthread.php?tid=849)



Z_ChangeStockCode.php does not rename image file - PakRicard - 04-12-2013 05:48 PM

Hi:

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 :-(

Code:
        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?


RE: Z_ChangeStockCode.php does not rename image file - PakRicard - 04-13-2013 05:50 PM

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?


RE: Z_ChangeStockCode.php does not rename image file - phil - 04-14-2013 06:01 PM

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 :

Code:
rwx r-x r-x


should do it ok.


RE: Z_ChangeStockCode.php does not rename image file - PakRicard - 04-16-2013 01:56 PM

Thanks Phil: Yes, it was permissions problem.

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

many thanks!


RE: Z_ChangeStockCode.php does not rename image file - phil - 04-16-2013 02:03 PM

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!!


RE: Z_ChangeStockCode.php does not rename image file - PakRicard - 04-19-2013 11:55 AM

Hi:

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.

Code:
        $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?


RE: Z_ChangeStockCode.php does not rename image file - billy000 - 02-19-2014 06:00 PM

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.