See if this helps. To use this script, you need to create a folder named
uploads where the uploaded files would be stored.
<?php
$maxsize=28480; // Set the maximum upload size in bytes
if (!$_POST['submit']) {
//print_r($_FILES);
$error=" ";
// This will cause the rest of the process to be skipped
//and the upload form displays
}
if (!is_uploaded_file($_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
$error = "<b>You must upload a file!</b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if($_FILES['upload_file']['type'] != "image/gif" AND
$_FILES['upload_file']['type'] != "image/pjpeg" AND
$_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
$error = "<b>You may only upload .gif or .jpeg files.<b><br /><br />";
unlink($_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
move_uploaded_file($_FILES['upload_file']['tmp_name'],
"uploads/".$_FILES['upload_file']['name']);
print "Thank you for your upload.";
exit;
}
else
{
echo ("$error");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP File Upload Script</title>
</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="50" />
<br />
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="Reset" value="Reset" />
</form>
</body>
</html>
This bit of code moves the uploaded file from a temporary directory into the uploads directory:
move_uploaded_file($_FILES['upload_file']['tmp_name'],
"uploads/".$_FILES['upload_file']['name']);
The MIME types included here are: .gif, .pjpeg and .jpeg but you may like to add more file formats.
This first line of the code defines the maximum file size:
$maxsize=28480; // Set the maximum upload size in bytes
You can edit the size to suit your needs.