Below is a script with basic error handling:
- renames files: 1.gif ... n.gif n - number of the last file
- checks if file exists
- checks mime format
- checks file size
- it handles up to 10 uploads
- displays upload summary (status of each upload + total number of uploaded files)
Enjoy :-)
<?
// (C) 2004 Piotr Szwajkowski
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$form = TRUE;
$upload_path = "c:\\apache\\htdocs\\test\
$fname = 1;
for ($i=0;$i<10;$i++)
{
list($problem, $error) = upload_file ($upload_path, $fname.".gif", $i);
if (!$problem)
{
$fname++;
}
else
echo $i.": ".$error."<br>";
}
echo "<br>";
echo --$fname." file(s) uploaded";
if ($form)
{
?>
<form enctype="multipart/form-da
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
0: <input class="file" type="file" name="file[0]"><br>
1: <input class="file" type="file" name="file[1]"><br>
2: <input class="file" type="file" name="file[2]"><br>
3: <input class="file" type="file" name="file[3]"><br>
4: <input class="file" type="file" name="file[4]"><br>
5: <input class="file" type="file" name="file[5]"><br>
6: <input class="file" type="file" name="file[6]"><br>
7: <input class="file" type="file" name="file[7]"><br>
8: <input class="file" type="file" name="file[8]"><br>
9: <input class="file" type="file" name="file[9]"><br>
<input type="submit" value="Submit Images">
</form>
<?
}
function upload_file ($upload_path, $fname, $fileid)
{
$problem = TRUE;
switch ($_FILES['file']['error'][
{
case UPLOAD_ERR_FORM_SIZE:
$error = "The uploaded file exceeds the 85kb";
break;
case UPLOAD_ERR_NO_FILE:
$error = "No file was uploaded";
break;
default:
$error = "";
}
//$fname = $_FILES['file']['name'][$f
$dest = $upload_path.$fname;
$tmpfile = $_FILES['file']['tmp_name'
if ($tmpfile)
{
$info = getimagesize($tmpfile);
$ftype = $info['mime'];
if ($ftype != "image/gif" && $ftype != "image/jpeg")
{
$error = "File type not supported";
$problem = TRUE;
}
else
{
if (file_exists($dest))
{
$error = "File already exists. (".$dest.")";
$problem = TRUE;
}
else
{
copy ($tmpfile,$dest);
$problem = FALSE;
}
}
}
return array($problem, $error);
}
?>
Main Topics
Browse All Topics





by: hernst42Posted on 2004-09-17 at 23:19:56ID: 12090629
You can try this modified version of the script:
]['size'] as $id => $size) {
tmp_name'] ; type']; size']; error'];
ame, $file_path); a><br><img src=$file_url height=\"210\" widt
ta" method="post">
<?php
if ($_FILES['userfile']) {
//to see the structure that generates PHP for uploaded files with an name ending with[] uncomment the following line:
echo '<pre>'; var_dump($_FILES); echo '</pre>';
$num = 0;
foreach($_FILES['userfile'
if ($size == 0) {
//no or empty file, skip it
continue;
}
$message .= do_upload($id, $upload_dir, $upload_url);
}
++$num;
}
function do_upload($id, $upload_dir, $upload_url) {
static $counter = 1;
$temp_name = $_FILES['userfile'][$id]['
$file_type = $_FILES['userfile'][$id]['
$file_size = $_FILES['userfile'][$id]['
$result = $_FILES['userfile'][$id]['
$file_name = $counter . '.gif';
$file_url = $upload_url.$file_name;
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
$message = "Invalid File Name Specified for filei $id.";
return $message;
}
//File Size Check
else if ( $file_size > 500000) {
$message = "The file size is over 500K for file $id.";
return $message;
}
//File Type Check
else if ( $file_type == "text/plain" ) {
$message = "Sorry, You cannot upload any script file" ;
return $message;
}
if (file_exists($file_path)) unlink($file_path);
$result = move_uploaded_file($temp_n
$message = ($result)?"File url <a href=$file_url>$file_url</
h=\"210\">" :
"Somthing is wrong with uploading a file.";
++$counter;
return $message;
}
?>
.....
<form name="uplVoad" id="upload" ENCTYPE="multipart/form-da
<span class="style1">Upload Slideshow Image<br>
</span>
<?php for ($i = 0; $i< 10; ++$i) { ?>File <?=$i?>:
<input type="file" class="userfile" name="userfile[]"><br>
<?php } ?>
<input type="submit" name="upload" value="Upload">
</form>
So the mostimportant thing to know is how the structure of uploaded files will look like if in the form the name is eneded with []
The files will be name from 1 to 10. If a user skips one field for files inbetween the sequence will not ge broken. This is done by the static variable $counter in do_upload as do up_load is only. If one of those files have failed it will be overwritten by the next file.