【发布时间】:2015-11-16 18:00:30
【问题描述】:
我在上传带有 html 表单的图像时遇到问题。当我在大约 3-5 秒后提交表单时,我在浏览器中收到以下错误:
ERR_CONNECTION_RESET
问题似乎出在 100kb 及更大范围内的图像上。较小的图像每次都提交,但这些较大的图像提交可能 10 次尝试中的 1 次。我在.htaccess 文件中为post max size、upload max file size 等设置了更高的值,但这并没有解决问题。
这是我的 HTML 表单:
<form method="post" name="update_form" action="php/editpost" enctype="multipart/form-data">
<div class="form-group">
<label for="img_upload">Select image</label>
<input type="file" name="img_upload" id="img_upload" class="form-control">
</div>
<div class="form-group">
<input class="btn btn-success btn-lg" type="submit" value="Update" name="edit_submit" id="edit_submit">
</div>
</form>
在 PHP 中,我使用“move_uploaded_file”将图像移动到它的目标目录,例如:
if ($_FILES["img_upload"]["name"]) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["img_upload"]["name"]);
$imgsrc = $target_file;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image and square
$check = getimagesize($_FILES["img_upload"]["tmp_name"]);
if($check !== false) {
$width = $check[0];
$height = $check[1];
if ($width != $height) {
$error = $error."<br />- The image is not square, please crop it and try again.";
$uploadOk = 0;
}
} else {
$error = $error."<br />- File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
chmod($target_file,0755); // Change the file permissions
unlink($target_file); // remove the file
}
// Check file size
if ($_FILES["img_upload"]["size"] > 1000000) {
$error = $error."<br />- Your image is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
$error = $error."<br>- Only JPG, JPEG & PNG images are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$fail .= "There were error(s) while uploading the image:<br />".$error;
}
// if everything is ok, try to upload file
else {
if (move_uploaded_file($_FILES["img_upload"]["tmp_name"], $target_file)) {
$sql = mysqli_query($link,"UPDATE `posts` SET `imagesrc`='$imgsrc' WHERE `postid`='$postid' LIMIT 1");
if ($sql) {
$success .= '- Image updated successfully.<br />';
} else {
$fail .= "- Failed to update the image.<br />";
}
}
}
}
header("location:../admincontrol?result_s=".urlencode($success)."&result_f=".urlencode($fail));
exit();
任何帮助将不胜感激,我包含了所有这些代码,但我认为它可能需要在 .htaccess 或 php.ini 中设置?
【问题讨论】:
-
小图上传成功了吗?
-
小于 1MB 的 >100kb 不太可能导致任何问题。想不出我见过的最大帖子大小为几百 KB 的任何 PHP 配置。
-
@user3187056 是的,每次上传非常小的图片,几乎每次上传超过 50kb 的图片,但仍然时不时地失败。 100kb 以上似乎只能工作 10 次,给予或接受。
-
你有另一台机器要测试吗?这可能来自您的本地计算机,而不是您的服务器
-
@SetSailMedia 谢谢就是这样,我的macbook是问题所在,不知道为什么。在另一台运行 ubuntu 的机器上对其进行了测试,并且每次都可以使用较大的图像。
标签: php html forms .htaccess ini