PHP Script to Delete Files on Server (from a folder)

PHP SCRIPT TO DELETE FILES ON SERVER?
(OR how to set up a script that ONLY deletes the contents of a folder and NOT the folder itself)

Objective: You need to create a PHP script and place it in your ROOT directory

Disclaimer: Read directions and MAKE SURE your path is correct or you could end up deleting stuff you didn’t want to
(in other words run a test after you’ve backed up your site)

———————————-
You need to create a PHP script and place it in your ROOT directory

1- to do this, create a text document and paste in the following code:

<?php
function remove($dirname = ‘.’)
{
        if (is_dir($dirname))
        {
                echo “$dirname is a directory.<br />”;

                if ($handle = @opendir($dirname))
                {
                        while (($file = readdir($handle)) !== false)
                        {
                                if ($file != “.” && $file != “..”)
                                {
                                        echo “$file<br />”;

                                        $fullpath = $dirname . ‘/’ . $file;

                                        if (is_dir($fullpath))
                                        {
                                                remove($fullpath);
                                                @rmdir($fullpath);
                                        }
                                        else
                                        {
                                                @unlink($fullpath);
                                        }
                                }
                        }
                        closedir($handle);
                }
        }
}
remove(‘test/’); // This is the directory folder that the files reside in to be deleted.
?>

—————-

2- make sure that you replace the part of the code that says: (with the folder name that you want your contents emptied FROM)

remove(‘test/’); // This is the directory folder that the files reside in to be deleted.

So to clarify, if your folder is located at:
(the folder that your want contents emptied from)

Example only:
http://www.simpleinbound.com/wp-content/cache/

then you need to add  wp-content/cache/ to the line, so that it looks like this

remove(‘wp-content/cache/‘); // This is the directory folder that the files reside in to be deleted.

———————-

3– Now SAVE this file, rename it emptycachescript.php, and upload to your ROOT diretcory

——————————–
4- put some contents in the folder that you want things deleted from (for purposes of testing)

(NOTE YOU CAN TEST IF THIS IS WORKING BY RUNNING THE SCRIPT THRU YOUR BROWSER)

SO you would type in:

http://www.yoursite.com/emptycachescript.php

Now go back and check, to see if the folder is now empty?
Yes? – cool then its working!

NOW that you’ve got the script, you can set it up to run thru your ISP, or cron or automated process plugin

_____________________________________________

WordPress users? I have added a solution for you
Please seed this post which details the process using the PHP script from this post

Related Posts Plugin for WordPress, Blogger...