Amit Shah

An IT Professional

PHP upload progress PDF Print E-mail
User Rating: / 9
A simple way to implement upload status for an administration panel. Based on AJAX and my JavaScript LoadVars class, this method doesn't need anything else to show uploading status on a simple form based html page.

What is this ?

It's another AJAX based application that can produce a usefull output with uploaded bytes informations.

How many limits has this method ?

  • user should have an AJAX compatible browser (maybe all recent browsers version are compatible)
  • you need to know and you need to access on server temporary upload folder with a PHP dedicated page
  • probably this method doesn't work on safe-mode configured server
  • informations are only for uploaded bytes and not for uploaded and total filesize (javascript secutiry reason)
  • it works probably only with few users at same time because dedicated temporary uploaded file name is really difficult to know

The idea !

When an enctype="multipart/form-data" is submitted, PHP have to wait the end of upload process.
But what really happen to the server ?
An incremental copy of the file is created while the upload process is waiting for the end, and this file will be the key for this application.
Another php file, during upload, should read the size of this file then should tell us its filesize!.
Sure, that php page that is uploading cannot tell us anything, but another page should!
Then with a JavaScript function and LoadVars object it's really simple call the other page.

PHP example page with multipart/form-data form

<title>Test Upload Progress</title>
<script type="text/javascript" src="LoadVars.js">
<!--// //--></script>
<script type="text/javascript" src="BytesUploaded.js">
<!--// //--></script>
<script type="text/javascript">
var bUploaded = new BytesUploaded('whileuploading.php');
<body style="font-family: sans-serif; font-size: 8pt;">
<form enctype="multipart/form-data" method="post" action="index.php"
<fieldset style="padding: 20px;">
<legend>Add a file</legend>
<input id="filename" type="file" name="gfile" />
<input type="submit" value="aggiungi file" />
<div id="fileprogress" style="font-weight: bold;"> </div>
<pre><?php var_dump($_FILES); ?></pre>

Important things are bolded and are the unique things you need to use this method.
Now it's time to look at the other file, whileuploading.php file, the one that should tell us upload status. 

PHP example page that should tell us upload informations

$tmpdir = 'C:/WINDOWS/TEMP';
// your PHP temporary upload folder ( without last slash )
// for this method is better set this folder in a dedicated one
// to be sure that in that folder there isn't any other php temporary file

// need a session to be really efficient
// and maybe a cleared stats
$UPM = new UploadProgressManager($tmpdir);
// new UploadProgressManager with temporary upload folder
if(($output = $UPM->getTemporaryFileSize()) === false)
// if UPM class cannot find the temporary file
$output = '&filesize=undefined';
// the output for LoadVars will be undefined
$output = '&filesize='.$output;
// else the output will be temporary file size
header('Content-Length: '.strlen($output));
// now headers to resolve browser cache problems
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
echo $output;
// and finally the output for LoadVars

To know more about UploadProgressManager PHP4 class visit the link. 

Originaly posted by Andrea Giammarchi on
< Prev   Next >

Tag Here