PHP 上传表单 - 无法上传 200kb 图像文件
我有一个上传 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该检查服务器上的 php.ini 文件,特别是以下参数:
post_max_size
upload_max_filesize
max_execution_time
max_input_time
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:
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.
隐藏表单字段标记是向浏览器建议这是允许的最大大小,但它不会更改服务器端设置。在对文件进行任何处理之前,您必须检查上传是否成功:
等等。文件上传错误常量的完整列表可在此处获取。
想象一下,如果该隐藏字段允许您覆盖服务器大小限制 - 即使您将限制设置为 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:
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?
检查 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.