douqiu1604 2017-08-15 16:22
浏览 46

用于文件上传的PHP脚本 - 无法上传大于~1MB的文件

I'm using a PHP script to upload doc/docx/pdf to my server. As long as the file size isn't bigger than 1MB, I don't have any issues with the upload, however the moment it's around 1MB, it just won't upload at all.

I updated the values in my .htaccess to the following:

php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_input_time 300
php_value max_execution_time 300
php_value memory_limit 96M

There are no errors or warnings in the log file. I'm using the below code to upload the files:

    $checkUpload = $_FILES["fileToUpload"]["name"];
    $target_dir = realpath(dirname(__FILE__)) . "/uploads/";
    $temp = explode(".", $_FILES["fileToUpload"]["name"]);
    $newfilename = 'newfilename' . end($temp);
    $target_file = $target_dir . $newfilename;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        $uploadOk = 0;
    }
    if (($imageFileType != "pdf") && ($imageFileType != "doc") && ($imageFileType != "docx")) {
        echo "wrong format!";
        die();
    }
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $newfilename)) {
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }

I have restarted the apache after updates and checked the max size with the below script:

$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
$upload_mb = min($max_upload, $max_post, $memory_limit);
echo $upload_mb;

The above confirms maximum size is 10M. I'm not even looking into uploading more than 5MB and can't figure out what the issue is.

E: I changed the values in php.ini, but it still doesn't work. I'm using PHP 7.0 with apache 2.4 on debian 8.

E2: I tried to upload the files using a WordPress plugin (Caldera Forms) and after a few battles this seems to be getting through to regular uploads folder (even with 4MB files). The issue is, I don't think using a plugin is a solution and I'd like to understand why I can't do it my way.

I'm currently getting the following warning:

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpThmBZJ' to '/var/www/mywebsite.com/uploads/form_uploads/1234.doc' in /var/www/mywebsite.com/public_html/wp-content/themes/mytheme/add_file.php on line 85, referer: http://mywebsite.com/

It would suggest a permission issue, but:

  • the caldera form files are still getting through
  • I made sure the apache is currently the owner of www and the relevant uploads folder
  • upload folder permissions are set to 755

Update: since I started receiving the above warning, I can't even upload the small files anymore.

Thanks for help, E.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染