Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete directory or files created with PHP (a.k.a permissions hell)

Here's the scoop: I need to be able to create folders using a PHP script and also to upload image files to those folders. Here is my code:

Creating a directory:

mkdir('[path]/images/foldername');  


Uploading Images:

if ($_FILES["file"]["error"] > 0 || $_FILES["file"]["type"] != "image/jpeg") // file must be valid and .jpg
{
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"] . '<br />';

    if(file_exists($path ."/" . $_FILES["file"]["name"]))
    {
        echo $_FILES["file"]["name"] . " already exists. ";
    }
    else
    {
        move_uploaded_file($_FILES["file"]["tmp_name"], $path ."/" . $_FILES["file"]["name"]);
        echo "Stored in: " . $path ."/" . $_FILES["file"]["name"];
    }
}

FTP editor gives these errors:

[L] DELE 20.jpg
[L] 550 Could not delete imagename.jpg: Permission denied

then

[L] RMD foldername
[L] 550 Can't remove directory: Directory not empty

I tried changing the permissions in my FTP editor, but got this error:

[L] SITE CHMOD 777 [path]/foldername [L] 550 Could not change perms on [path]/foldername: Operation not permitted
I tried using SSH with Putty to delete the file, but that did not work either.

Please help me!

like image 920
Nate Avatar asked May 18 '12 22:05

Nate


People also ask

Can not delete folder in cPanel?

From the File Manager in cPanel, highlight the folder you want to delete. Click the Delete link from the tool bar at the top of the page. A pop-up will appear asking you to confirm that you want to delete the folder. If you want to permanently delete the file, click the Skip trash and permanently delete the file box.

How to delete all files using command prompt?

The del command displays the following prompt: Are you sure (Y/N)? To delete all of the files in the current directory, press Y and then press ENTER. To cancel the deletion, press N and then press ENTER.


3 Answers

After you move the uploaded file, try doing:

@chmod($path ."/" . $_FILES["file"]["name"], 0777);

or something. You might want to change the permissions to something better.

like image 77
Mitch Dempsey Avatar answered Sep 16 '22 22:09

Mitch Dempsey


It looks like your PHP installation runs as a different user than your FTP session. Hence your php upload script can create files that your ftp user can't touch ;-).

There are essentially three ways to deal with the problem:

  1. Write a php-script that deletes the files you want to delete. Use unlink() for that. Google will also give you some recursive implementations.
  2. Change the the permissions on the file (using chmod. Be careful, you easily make the files accessible to people or processes that should not have this possibility. Don't do this in a production environment)
  3. Make sure your ftp user and your php user are the same. You will need to be root on your target machine or you need a friendly admin on the machine to set it up accordingly.
like image 42
yankee Avatar answered Sep 19 '22 22:09

yankee


You could change the user account the FTP server is running under to use the same user as your PHP script (possibly www-data or httpd)

like image 45
Sergi Avatar answered Sep 18 '22 22:09

Sergi