Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post

    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

  • #2
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #4
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    120 (06-30-2010)

  • #6
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #7
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #8
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #9
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #10
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #11
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #12
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #13
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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

  • #14
    Regular Coder
    Join Date
    Jan 2010
    Posts
    161
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #15
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •