【问题标题】:Cannot upload files exceeding 1MB in size - PHP webserver无法上传大小超过 1MB 的文件 - PHP 网络服务器
【发布时间】:2017-04-19 13:02:49
【问题描述】:

我目前对我拥有的网络服务器有点噩梦。我几乎搜索了我能找到的每个 Stack Overflow 页面,但仍然没有成功。

我有一个在本地运行的网络服务器,我会首先测试这些设置,一切正常,但是每当我将相同的设置应用到我的主服务器时,就没有这样的运气了。

我已经回到基础,只是使用一个简单的 html 页面和 php 页面来处理请求,但这仍然失败,超过 1MB。

作为参考,这是我的 html 脚本:

<!DOCTYPE html>
    <html>
    <body>

    <form action="test.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>

    </body>
</html>

这是我处理请求的 test.php 页面:

<?php
$target_dir = "";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 8000000) { //8mb
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

这只是来自 w3 Schools 的基本 php 图片上传脚本 - 这就是这个问题让我陷入的困境!

我尝试过的:

  • 编辑 php.ini 文件然后重启 apache
  • 更改 html 表单,使其在表单标题中包含 enctype="multipart/form-data"
  • 将 .htaccess 添加到文件所在的根 www/html 文件夹
  • 更改指定最大允许图像大小的上传脚本位:

我得到的错误是:

Warning: getimagesize(): Filename cannot be empty in /var/www/html/test.php on line 8
File is not an image.Sorry, your file was not uploaded.

谁能帮帮我?我也尝试过禁用 mod_security,但我什至在我的 apache2 服务器上都找不到。

非常感谢。

【问题讨论】:

  • 那么你在本地服务器和主服务器上得到的关于getimagesize() 的错误是什么?您通过上传大于 1 MB 的文件得到
  • 可能您超出了默认时间限制,因为您的文件超过 1MB。尝试使用set_time_limit(0); 将时间限制设置为无限制
  • 不,我只有主服务器上的错误。当我在笔记本电脑上运行的网络服务器上尝试它时,一切都很好,就在我将它推送到主服务器时。查看 set_time_limit(0) - 我将其添加到 php 脚本但没有成功。

标签: php html apache .htaccess mod-security


【解决方案1】:

您可以通过以下两种方式更改上传限制

i)改变php.ini中upload_max_filesize的值

ii)使用 ini_set("upload_max_filesize","30M");

【讨论】:

  • 你可以在哪里设置 ini_set("upload_max_filesize","30M"); ?
  • 哦,没关系,也将其添加到 php 文件中,但无济于事 - 仍在寻找解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2017-04-05
相关资源
最近更新 更多