Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2010, 02:13 PM   PM User | #1
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
PHP Unlink Permission Error

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

Code:
$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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 03:20 PM   PM User | #2
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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.
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 04:10 PM   PM User | #3
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 04:17 PM   PM User | #4
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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.
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 04:20 PM   PM User | #5
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Quote:
Originally Posted by 120 View Post
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.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Users who have thanked _Aerospace_Eng_ for this post:
120 (06-30-2010)
Old 06-30-2010, 04:23 PM   PM User | #6
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 04:25 PM   PM User | #7
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
Quote:
Originally Posted by _Aerospace_Eng_ View Post
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.
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 04:35 PM   PM User | #8
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 04:50 PM   PM User | #9
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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.
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 05:03 PM   PM User | #10
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 05:08 PM   PM User | #11
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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).
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 05:17 PM   PM User | #12
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 05:22 PM   PM User | #13
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
In theory if a file has 777 *anyone* can do anything to it. What happens if you 777 the directory momentarily?
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 06-30-2010, 05:30 PM   PM User | #14
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
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
ROYW1000 is offline   Reply With Quote
Old 06-30-2010, 05:38 PM   PM User | #15
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
What about:

PHP Code:
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 :-/
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:22 PM.


Advertisement
Log in to turn off these ads.