Home arrow PHP arrow Page 5 - PHP: Creating Word Documents without .COM Objects

Deleting Temporary Files in the Server Upload directory - PHP

If you are looking for a solution to create MS Word documents without the use of .COM objects, then this tutorial will be helpful. In it, you will learn how to build an actual PHP application that can convert a .txt file into a .doc file online.

TABLE OF CONTENTS:
  1. PHP: Creating Word Documents without .COM Objects
  2. Strategy for creating this application
  3. The Basic Web Form Source Code
  4. PHP Script: Validation, File get Contents and Writing to .doc file
  5. Deleting Temporary Files in the Server Upload directory
By: Codex-M
Rating: starstarstarstarstar / 5
July 26, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: