PHP Upload Form - can't upload 200kb image file
I have a form to upload 2 files. They can upload one, or just upload both at the same time. When I upload 2 small image files (both under 100kb), they work perfect. But if one file is larger, say around 200kb, it doesn't work. I already set the max value to "100000" in the hidden tag below, so I'm not sure what else I can do to fix this?
<form enctype="multipart/form-data" action="upload.php" method="post">
<b>Image File</b><br />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<b>Large Image</b>: <input type="file" name="uploadedfile1" size="30" /><br />
<b>Thumb Image</b>: <input type="file" name="uploadedfile2" size="30" /><br />
<center><input type="submit" name="submit" value="submit" class="button"></center>
</form>
When it's processed, it goes to this php code:
$uploadedfileBase1 = basename($_FIL开发者_StackOverflow中文版ES['uploadedfile1']['name']);
$uploadedfileTemp1 = $_FILES['uploadedfile1']['tmp_name'];
$uploadedfileBase2 = basename($_FILES['uploadedfile2']['name']);
$uploadedfileTemp2 = $_FILES['uploadedfile2']['tmp_name'];
// Large Image
$target_path_large = $target_path . "large/" . $uploadedfileBase1;
if(move_uploaded_file($uploadedfileTemp1, $target_path_large)) {
echo "<p>The <b>large</b> file \"$uploadedfileBase1\" has been uploaded.</p>";
} else{
echo "<p>There was an error uploading the <b>large</b> file <i>$uploadedfileBase1</i>, please try again!</p>";
}
// Thumb Image
$target_path_thumb = $target_path . "thumbs/" . $uploadedfileBase2;
if(move_uploaded_file($uploadedfileTemp2, $target_path_thumb)) {
echo "<p>The <b>thumbnail</b> file \"$uploadedfileBase2\" has been uploaded.</p>";
} else{
echo "<p>There was an error uploading the <b>thumbnail</b> file <i>$uploadedfileBase2</i>, please try again!</p>";
}
Thank you for reading!
You should check the file php.ini at your server, specially the following parameters:
post_max_size
upload_max_filesize
max_execution_time
max_input_time
memory_limit
In your case, the problem maybe about too small post_max_size & upload_max_filesize.
EDIT: Now I notice it, you yourself define MAX_FILE_SIZE = 100000 bytes < 100 KB in the hidden field. So your upload file can not exceed 100kB for sure. If you want to upload larger file, you must increase that value.
The hidden form field tag is a suggestion to the browser that this is the max allowed size, but it will not change the server-side settings. Before doing any processing on your files, you HAVE to check if the upload succeeded:
if ($_FILES['uploadedfile1']['error'] !== UPLOAD_ERR_OK) {
die("file #1 failed with error code " . $_FILES['uploadedfile1]['error']);
}
and so on. The complete list of error constants for file uploads is available here.
Imagine if that hidden field allowed you to override the server size limit - what would there be to stop someone from upload a terabyte-sized file, even though you'd set the limit to 10 megabytes?
Check the values you have for upload_max_filesize and post_max_size in your php.ini. Make sure they are larger than the file you are uploading.
精彩评论