无法使用max_filesize POST表单

时间:2015-04-22 07:16:38

标签: php html file-upload

上传文件工作正常,文件大小小于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>

1 个答案:

答案 0 :(得分:3)

在上传脚本之前,您可以在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)以使用新配置。