PHP上传表格 - 无法上传200kb图像文件

时间:2011-02-15 02:31:03

标签: php file forms upload size

我有一个表单来上传2个文件。他们可以上传一个,或者只是同时上传两个。 当我上传2个小图像文件(均低于100kb)时,它们的工作非常完美。但是如果一个文件较大,比如大约200kb,它就不起作用了。我已经在下面的隐藏标签中将最大值设置为“100000”,所以我不确定我还能做些什么来解决这个问题?

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

当它被处理时,它会转到这个PHP代码:

$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>";
}

感谢您阅读!

3 个答案:

答案 0 :(得分:4)

您应该检查服务器上的文件php.ini,特别是以下参数:

  1. 的post_max_size

  2. 的upload_max_filesize

  3. 的max_execution_time

  4. max_input_time设置

  5. memory_limit的

  6. 在你的情况下,问题可能是关于post_max_size&amp;的upload_max_filesize。

    编辑:现在我注意到了,你自己定义MAX_FILE_SIZE = 100000字节&lt;隐藏字段中的100 KB。因此,您的上传文件肯定不会超过100kB。如果要上传较大的文件,则必须增加该值。

答案 1 :(得分:1)

隐藏表单字段标记是向浏览器建议这是允许的最大大小,但它不会更改服务器端设置。在对文件进行任何处理之前,您必须检查上传是否成功:

if ($_FILES['uploadedfile1']['error'] !== UPLOAD_ERR_OK) {
    die("file #1 failed with error code " . $_FILES['uploadedfile1]['error']);
}

等等。文件上传的完整错误常量列表可用here

想象一下,如果隐藏字段允许您覆盖服务器大小限制 - 即使您将限制设置为10兆字节,还有什么可以阻止某人上传一个TB级文件?

答案 2 :(得分:1)

检查php.ini中upload_max_filesizepost_max_size的值。确保它们比您上传的文件大。

相关问题