【问题标题】:PHP Upload Form - can't upload 200kb image filePHP 上传表单 - 无法上传 200kb 的图像文件
【发布时间】:2011-06-27 07:55:02
【问题描述】:

我有一个表格可以上传 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>";
}

感谢您的阅读!

【问题讨论】:

  • 我刚刚检查了我的 php.ini,发现以下值(我认为这些值都很慷慨,所以可能不是原因..?):post_max_size (8M), upload_max_filesize (2M ), max_execution_time (30), max_input_time (60), memory_limit (24M)

标签: php file forms upload size


【解决方案1】:

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

  1. post_max_size

  2. upload_max_filesize

  3. max_execution_time

  4. 最大输入时间

  5. memory_limit

在您的情况下,问题可能与 post_max_size 和 upload_max_filesize 太小有关。

编辑: 现在我注意到了,您自己在隐藏字段中定义了 MAX_FILE_SIZE = 100000 bytes

【讨论】:

  • 我刚刚检查了我的 php.ini,发现以下值(我认为这些值都很慷慨,所以可能不是原因..?):post_max_size (8M), upload_max_filesize (2M ), max_execution_time (30), max_input_time (60), memory_limit (24M)
  • @Jay:抱歉,我只是注意到您在隐藏字段中设置了“MAX_FILE_SIZE= 100000”。这意味着文件不能超过100K,那么这里的问题是什么?
  • 哦!好吧,这对我来说很愚蠢+_+。已修复并现在像魅力一样工作,非常感谢! :)
【解决方案2】:

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

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

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

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

【讨论】:

  • 嗯,我收到了失败的错误,但我不知道为什么...当我通过 FTP 检查时,大文件没有上传,但较小的文件可以。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2012-02-06
    • 2013-03-25
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    相关资源
    最近更新 更多