【问题标题】:Unable to POST form with max_filesize无法使用 max_filesize 发布表单
【发布时间】:2015-06-29 16:26:18
【问题描述】:

上传文件工作正常,文件大小小于 2.9 MB,但我的 phpinfo (localhost) 显示 upload_max_filesize 64M

当尝试上传较大的文件时,表单提交后 $_POST 为空且没有上传文件。

这是我的代码:

        <?php
            function fileUpload($attachment){
                $target_file = UPLOADDIR.basename($attachment["name"]);
                if (file_exists($target_file)) {
                    return "Sorry, file already exists.";
                }
                if (move_uploaded_file($attachment["tmp_name"], $target_file)) {
                    return "The file ". basename( $attachment["name"]). " has been uploaded.";
                } else {
                    return $attachment;
                    return "Sorry, there was an error uploading your file.";
                }
            }
            if(isset($_POST["submit"])) {
                fileUpload($_FILES['fileToUpload'])
            }
        ?>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" 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>

【问题讨论】:

  • 查看php执行时间
  • 你也应该检查 post_max_size stackoverflow.com/questions/6135427/…
  • 你的意思是 max_execution_time 它目前是 120。我只是在寻找最大大约 5 MB 的文件上传大小。
  • @steven post_max_size 只有 3M。这是个问题吗?我需要增加它吗?
  • 是的,它应该等于或大于upload_max_filesize

标签: php html file-upload


【解决方案1】:

在上传脚本之前,您可以在您的 php 代码中设置最大上传大小

ini_set('upload_max_filesize', '10M');

或 您需要在 php.ini 中设置 upload_max_filesize 和 post_max_size 的值:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

然后运行你的代码

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

把它放在你的 PHP 脚本的顶部,让你的脚本松散!

如果您对 php.ini 文件进行了任何更改或修改,则需要重新启动 HTTP 服务器(或 localhost)以使用新配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2017-01-11
    • 2018-06-29
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多