Header
 
» IT tipps and howto's

PHP Script: Delete files and folders created by Apache user

Last Update: 18. Jan 2010

Download Script delapacheuserfiles

There is one problem on shared hosting servers with Apache and PHP running on it: If a PHP script creates files and folders the owner of those created files is the user under which Apache is running, that's mostly www-data or wwwrun. User's accessing their webaccount by ftp and wanting to delete such files receive a permission denied error - because (of course) they're not allowed to delete those files.

I know, there are some solutions like suphp but that's not often the case so let's stick with this way of setup.

Here a typical output of a ls -l where we see the files and folders created by a PHP script:

-rw-r--r-- 1 www-data www-data 0 2010-01-16 18:30 file1
-rw-r--r-- 1 www-data www-data 0 2010-01-16 18:30 file2
-rw-r--r-- 1 www-data www-data 0 2010-01-16 18:30 file3
-rw-r--r-- 1 www-data www-data 0 2010-01-16 18:30 file4
-rw-r--r-- 1 www-data www-data 0 2010-01-16 18:30 file5
drwxr-xr-x 2 www-data www-data 4.0K 2010-01-16 18:30 testfolder1
drwxr-xr-x 2 www-data www-data 4.0K 2010-01-16 18:30 testfolder100
drwxr-xr-x 2 www-data www-data 4.0K 2010-01-16 18:30 testfolder2
drwxr-xr-x 2 www-data www-data 4.0K 2010-01-16 18:30 testfolder3
drwxr-xr-x 2 www-data www-data 4.0K 2010-01-16 18:30 testfolder40

During the last weeks I've tried to find already prepared scripts and also to write my own script using rmdir and chown - but I always struggled in the way of finding the actual files and directories which had to be deleted. Until today - and the solution is so easy! I use exec to execute a shell command (find)... why didn't I think of that before?

You simply have to download the script, unzip it, upload it into the directory where you want to have the specific apache deleted and execute it on your browser.

Well here's the script itself:

<?php

########################################################
# delapacheuserfiles.php
#
# Author: Claudio Kuenzler
# Company: Nova Company GmbH www.novacompany.ch
# Purpose: Deletes files and folders created by Apache user
#
# Version History
# 20100116 Script programmed
# 20100118 Bugfix for current dir (could not be deleted)
# now set to chmod777 so ftp user can delete
########################################################

// Files
exec("find . -type f -user www-data", $fileresult);
echo "Die folgenden Dateien wurden gefunden:<br>";
foreach ($fileresult as $found) {
echo "<br> $found";
}

foreach ($fileresult as $file) {
unlink("$file");
}

// Folders
exec("find . -type d -user www-data", $folderresult);

if ($folderresult[0] == ".") {
chmod("$folderresult[0]", 0777);
unset($folderresult[0]); // This removes the current directory from the list
}

echo "<br>Die folgenden Ordner wurden gefunden:<br>";
foreach ($folderresult as $folder) {
echo "<br> $folder";
}

foreach ($folderresult as $folder) {
chmod("$folder", 0777);
rmdir("$folder");
}

?>

 


Go to Homepage home RSS Feed
About ck about
Linux Howtos how to's

 

 

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

10036 Days
until Death of Computers