...

View Full Version : PHP Unlink Permission Error



ROYW1000
06-30-2010, 02:13 PM
Hi

I have this script that reads data and unlinks files.

The directory is set as 755 and all the other directories but the image itself is set at 644 which I would prefer to keep like this but I get the following error.

Is it possible to add a chmod to this script and if so how so that it can delete them.

I have had a few attempts at things like and put them after
while ($current = mysql_fetch_assoc($images)) but none of these worked yet.

chmod('mainimage', 0777);
chmod($current 'mainimage', 0777);
chmod($current['mainimage'], 0777);

Warning: unlink(shopimages/products/normal/BNCTO-40.jpg) [function.unlink]: Permission denied in /var/www/vhosts/test.com/httpdocs/delete_by_id.php on line 32



$images = mysql_query('SELECT mainimage, thumbnail FROM products WHERE productID IN ('.$data.')') or exit(mysql_error());
while ($current = mysql_fetch_assoc($images))
{
if (trim($current['mainimage']) != '' && is_file($current['mainimage']))
{
unlink($current['mainimage']);
}

if (trim($current['thumbnail']) != '' && is_file($current['thumbnail']))
{
unlink($current['thumbnail']);
}
}


Thanks
Roy

120
06-30-2010, 03:20 PM
One thing springs to mind - who is the owner/group of 'shopimages/products/normal/BNCTO-40.jpg' ???

Personally, I'd set the owner so it's the same as the user running your web server. You've not mentioned if you are Win or Nix. I could not hope to tell you how to do that with a windows server/platform (or if you can) - but on Linux Apache usually runs as 'nobody', and I'd consider setting the owner of files you wish to delete using php to 'nobody'. Normally for files uploaded via php forms this happens automatically - but this probably won't be the case if the files are FTP'd into place.

ROYW1000
06-30-2010, 04:10 PM
Hi

I just used WinScp and it says the Owner for all the files is the name of the website.

This includes all the folders that lead to the product images and the images.

How do you go about changing them and do I just change the actual Images to nobody.

Thanks
Roy

120
06-30-2010, 04:17 PM
My guess is you are using windows to transfer files to a Linux host? Yes?

My next guess, chances are the files are owned by the account name you have on the host, and Apache (which usually runs as 'nobody') is unable to delete them because of the ownership issue. This is nothing more than an educated guess.

For me I would run this on the host: "chown -R nobody:nobody /path/to/files/*" and check again, **but** you probably can't do that?

I'll see if I can find a copy of WinSCP on a box here and look to see how (or if) it can do this.

_Aerospace_Eng_
06-30-2010, 04:20 PM
My guess is you are using windows to transfer files to a Linux host? Yes?

My next guess, chances are the files are owned by the account name you have on the host, and Apache (which usually runs as 'nobody') is unable to delete them because of the ownership issue. This is nothing more than an educated guess.

For me I would run this on the host: "chown -R nobody:nobody /path/to/files/*" and check again, **but** you probably can't do that?

I'll see if I can find a copy of WinSCP on a box here and look to see how (or if) it can do this.

I know you can right click on a file and change the owner including its permissions. You might be able to change the permissions recursively if you changed the properties on a folder. I'm not sure, never had to do that.

120
06-30-2010, 04:23 PM
OK, found a copy of WinSCP. If you log onto the remote host, in the right hand pane you'll have a list of files on the server. One of the columns is called 'user' (next to rights). Navigate to "shopimages/products/normal/BNCTO-40.jpg" and check the user. You can change this by right clicking (highlighting multiple entries if you need to), select 'properties' from the pop-out, then change the owner/group which are just above the permissions :-)

Give that a whizz on that single file and see if makes a difference.

HTH

120
06-30-2010, 04:25 PM
I know you can right click on a file and change the owner including its permissions. You might be able to change the permissions recursively if you changed the properties on a folder. I'm not sure, never had to do that.

Spot on. :-) Thanks. Suspect it has munged user/group from Windows and just needs setting to the same user httpd is running as.

ROYW1000
06-30-2010, 04:35 PM
Hi

In WinSCP those columns dont seem to be highlighted to allow me to change.

The files are originally put there from a Windows PC using WS-FTP PRO.

Not sure if you know of any other software that may help or if it can be done in WSFTP PRO or why WinScp wont allow the change.

Roy

120
06-30-2010, 04:50 PM
Not sure of your best fix here if I am honest. With 644 permissions on files only the original owner is going to be able to delete them via a script. changing them to 666 will probably let you delete them - it may even allow you to change the owner (and then put them back to 644).

You can be pretty sure your original issue relates to the ownership and permissions of these files.

ROYW1000
06-30-2010, 05:03 PM
Hi

I have just upgraded to the latest version of WinScp and now the boxes are available.

Now when I try to change owner to nobody it says "Current session allows changing ownership by UID only. It was not possible to resolve UID from account name "nobody Specify UID explicitly instead.

I then tried changing it to 0777 but still same error message. I have also added the name of the group in as well and that is psacln maybe this is due to it being on a Dedicated Server running plesk to I assume this means the psa.

But whatever I have tried it wont let me change the owner so far.

Roy

120
06-30-2010, 05:08 PM
OK, if you use a simple FTP client like Filezilla, are you able to change the permissions to 666 or even 777 on that file?

You *may* need to change the permissions on the containing directory to do this (you can always change it back afterwards).

ROYW1000
06-30-2010, 05:17 PM
Hi

I can change the permissions ok with WinScp and moved them both to 777.

I then tried the script again and got the same message.

Warning: unlink(shopimages/products/normal/BNCTO-40.jpg) [function.unlink]: Permission denied in /var/www/vhosts/test.com/httpdocs/delete_by_id.php on line 31

So maybe it not the permissions still, maybe its the owner but still cant work out how to change this.

Roy

120
06-30-2010, 05:22 PM
In theory if a file has 777 *anyone* can do anything to it. What happens if you 777 the directory momentarily?

ROYW1000
06-30-2010, 05:30 PM
Hi

The directory structire is as follows:

shopimages/products/thumbnails
shopimages/products/normal

When I changed the permission to 777 on shopimages it did not delete the file.

When I changed the permission of shopimages/products to 777 it did not delete the file.

When I changed the permission of shopimages/products/thumbnails to 777 it did delete the file.

Therefore is it possible to add this to the script and change back afterwards.

Roy

120
06-30-2010, 05:38 PM
What about:


chmod("shopimages/products/thumbnails", 0777);
//your code
chmod("shopimages/products/thumbnails", 0755);
??? cant say it will work with the permissions the server is running as.

BTW, there is also a php chown command: http://www.php.net/manual/en/function.chown.php
but if you don't have permission to write, it probably won't work :-/

ROYW1000
06-30-2010, 07:04 PM
Hi

With the script calling this am I not able to chmod the files that are required to be deleted from the Array as 777.



$images = mysql_query('SELECT mainimage, thumbnail FROM products WHERE productID IN ('.$data.')') or exit(mysql_error());
while ($current = mysql_fetch_assoc($images))
{
if (trim($current['mainimage']) != '' && is_file($current['mainimage']))
{
unlink($current['mainimage']);
}

if (trim($current['thumbnail']) != '' && is_file($current['thumbnail']))
{
unlink($current['thumbnail']);
}
}

Thnaks
Roy

120
06-30-2010, 10:07 PM
Truth is, you either need to have the right owner/permissions or open things up with 777 to let php/apache do what you want to do.

There may be other options - like ensuring the files have the right owner/permissions on them initially - but the permissions you have are doing the job they are required to do.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum