Check uploaded file size and delete the uploaded file in PHP

Description

The following code shows how to check uploaded file size and delete the uploaded file.

Example


<?php/*from  w  w w.java2 s.  c o  m*/
$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>
<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>




















Home »
  PHP Tutorial »
    Form »




PHP Form
Form Demo