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

发布于 2024-10-17 22:57:57 字数 1848 浏览 0 评论 0原文

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

感谢您的阅读!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2024-10-24 22:57:57

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

  1. post_max_size

  2. upload_max_filesize

  3. max_execution_time

  4. max_input_time

  5. memory_limit

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

编辑:现在我注意到了,你自己定义了 MAX_FILE_SIZE = 100000 字节 <隐藏字段中有 100 KB。所以你上传的文件肯定不能超过100kB。如果您想上传更大的文件,则必须增加该值。

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.

墨小墨 2024-10-24 22:57:57

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

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

等等。文件上传错误常量的完整列表可在此处获取。

想象一下,如果该隐藏字段允许您覆盖服务器大小限制 - 即使您将限制设置为 10 MB,如何才能阻止某人上传 TB 大小的文件?

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?

万人眼中万个我 2024-10-24 22:57:57

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文