Processing an uploaded file : File Upload « Form « PHP






Processing an uploaded file

 
<?php
$maxsize=28480; 
if (!$HTTP_POST_VARS['submit']) {
    $error=" ";
}
if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']) AND !isset($error)) {
    $error = "<b>You must upload a file!</b><br /><br />";
    unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
    unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
    move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'],
                       "uploads/".$HTTP_POST_FILES['upload_file']['name']);
    print "Thank you for your upload.";
    exit;
}
else
{
    echo ("$error");
}
?>

<html>
<head></head>
<body>
<form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>"
method="post" enctype="multipart/form-data">
    Choose a file to upload:<br />
    <input type="file" name="upload_file" size="80">
    <br />
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>
  
  








Related examples in the same category

1.File Upload Script
2.File Uploader
3.File Uploading Error Constants in PHP
4.Simple File Upload Form