Of course, since you are allowing text and doc files to be uploaded/created, they will clog that directory for some time and consume a lot of disk space. Below is the script that will delete these files (.txt and .doc). <?php //This is variation of the public domain source code found here: // http://www.jonasjohn.de/snippets/php/delete-temporary-files.htm // Define the folder to clean // (keep trailing slashes) // You need to run this script using your web hosting cron tab (consult your web hosting agency for details) at least every 10 minutes. // This script will only delete files created and uploaded if the file is more than 3 minutes old. //Define path, YOU NEED TO CHANGE THIS PATH! $uploadfolder = '/home/www/php-developer.org/convert-txt-to-msword/upload/'; //Delete MS Word files first $fileTypes1 = '*.doc'; // Define the expiration minutes , default 3 minutes $expire_time1 = 3; // Find all MS Word files in the path foreach (glob($uploadfolder . $fileTypes1) as $Filename1) { // Read file creation time $FileCreationTime1 = filectime($Filename1); // calculate the age of the files. $FileAge1 = time() - $FileCreationTime1; // Condition to evaluate age of files if ($FileAge1 > ($expire_time1 * 60)){ // If the file is more than 3 minutes old, then schedule it to be deleted in the next cron job execution of this script. unlink($Filename1); } } //Same thing above, but this will delete only text files $fileTypes2 = '*.txt'; $expire_time2 = 3; foreach (glob($uploadfolder . $fileTypes2) as $Filename2) { $FileCreationTime2 = filectime($Filename2); $FileAge2 = time() - $FileCreationTime2; if ($FileAge2 > ($expire_time2 * 60)){ unlink($Filename2); } } ?> Implementation and the Complete Script You can download the complete source code and see an actual implementation here: http://www.php-developer.org/convert-txt-to-msword/ The deletefiles.php is the actual PHP script that you will need to run using your web hosting cron tab. It is recommended that you change the file name from deletefiles.php to another name for security reasons. After downloading the file, extract it and upload the folder to the root directory of your server. In the upload folder, an .htaccess has been added to prevent browser execution of text files. This is a security feature in case someone uploads a text file which in reality is not a text file. You might as well try uploading text files to the actual page here: http://www.php-developer.org/convert-txt-to-msword/ so that you can see how the web application works as planned. To download the MS Word file, you will simply right click and save as the appropriate file type to your desktop.
blog comments powered by Disqus |
|
|
|
|
|
|
|