EZTwainX Uploading - PHP Example Script

See also: getting started, properties, methods, events, deployment

Here is essentially the PHP script we use to accept uploads from our demo page and client-side test programs.
$uploads is a variable that holds the destination directory for uploaded files.
The uploaded file is always given the name 'uploaded' plus the extension of the uploaded file. So if a file is uploaded in JPEG format with filename given as userform1.jpg, it will be stored on the server as uploaded.jpg in the folder specified by $uploads.
There is a check that rejects files larger than 1MB, and there is logic to reject files that are not in one of the allowed formats.
This script accepts multiple uploads, but modifying it to store them under different names is left as an exercise for the reader...

<html>
<head>
<title>Upload Received</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Cache-Control" content="no-cache">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
error_reporting
(E_ALL); ini_set('display_errors', 1);

function
handle_file($field)
{
    
$filename = $_FILES[$field]['name'];
    
$filetype = $_FILES[$field]['type'];
    
$filesize = $_FILES[$field]['size'];
    
$tempname = $_FILES[$field]['tmp_name'];
    
$uploads = "uploads";
    echo
"upload submitted.<BR>Field Name: $field, Field Elements:<BR>";
    echo
"filename: $filename<BR>";
    echo
"filetype: $filetype<BR>";
    echo
"filesize: $filesize<BR>";
    echo
"tempname: $tempname<BR>";
    echo
"<BR>";
    
$newname = $uploads."/uploaded".substr($filename, -4);
    if (!
file_exists($uploads)) {
        if (!
mkdir($uploads, 0755)) {
            echo
'mkdir("'.$uploads.'"); failed.<br>';
        }
    } else {
        if (!
chmod($uploads, 0755)) {
            echo
'chmod("'.$uploads.'"); failed.<br>';
        }
    }
    
//echo "file_exists: ".file_exists($tempname).'<br>';
    
echo "new name =".$newname."<BR>";
    if (
$filesize > (1024*1024)) {
        echo
'<font color="#FF0000">Error: File exceeds maximum allowed size (1MB)</font><br>';
    } else if (
$filetype != "image/jpeg" &&
        
       $filetype != "image/gif" &&
        
       $filetype != "image/png" &&
        
       $filetype != "image/tiff" &&
        
       $filetype != "application/pdf") {
        echo
'<font color="#FF0000">Error: File is not one of the allowed types (JPEG, GIF, PNG, TIFF, PDF)</font><br>';
    } else if (
move_uploaded_file($_FILES[$field]['tmp_name'], $newname)) {
        echo
$filename." received and stored!<BR>";
        if (
$filetype == "application/pdf") {
            echo
'<br><a href='.$newname.'>'.$newname.'</a><br>';
        } else {
            echo
'<br><img src="'.$newname.'">';
        }
    } else {
        echo(
'moving <font color="#FF0000">'.$filename."</font> to '.$newname.' failed.<BR>");
    }
}

if (isset($_FILES['file'])) {
    
handle_file('file');
} else {
    for (
$i = 1; isset($_FILES['file'.$i]);$i++) {
        
handle_file('file'.$i);
    }
}
?>
</body>
</html>