PHP Upload Form - can't upload 200kb image fil

2019-06-10 01:46发布

问题:

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($_FILES['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!

回答1:

You should check the file php.ini at your server, specially the following parameters:

  1. post_max_size

  2. upload_max_filesize

  3. max_execution_time

  4. max_input_time

  5. 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.



回答2:

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?



回答3:

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.