php file upload error of 1 - What is correct way to use php_ini?

扶醉桌前 提交于 2020-12-09 19:27:06

问题


I'm having issues uploading a pdf to my server. The upload_max_filesize is 2M and the file(s) are more then that, around 4M. I found a post with a similar issue to mine here

$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size

What I can gather from php.net for the correct usage of ini_set commands is this, which I am currently using.

    ini_set('upload_max_filesize', 100000000);
    ini_set('post_max_size', 110000000);
    ini_set('memory_limit', 120000000);
    ini_set('max_input_time', 20);

But in the link I posted it seems they are using a different method (if they aren't just summarizing the correct code that is). But It seems my code isn't working as is either. I have <?php phpinfo(); ?> at the bottom of my page and it says the upload_max_filesize is still 2M. Am I using the correct syntax for ini_set? or is my issue with upload my pdfs something else?

My code handling the upload is

    //======================pdf upload=====================     
    if ($_POST['erasePDF'] == "Yes") //checking if erase box was checked and erasing if true
    {   
        if (file_exists($pdf))
        {
            unlink( $pdf );
            $pdf = "";
        }
    }   

    print_r($_FILES['pdf']);
    if (!empty($_FILES['pdf']['name'])) //checking if file upload box contains a value
    {
        $saveDirectoryPDF = 'pdfs/';            //name of folder to upload to
        $tempName = $_FILES['pdf']['tmp_name']; //getting the temp name on server
        $pdf = $_FILES['pdf']['name'];      //getting file name on users computer

        $test = array();
        $test = explode(".", $pdf);
        if((count($test) > 2) || ($test[1] != "pdf" && $test[1] != "doc" && $test[1] != "docx")){
            echo "invalid file";
        }else{

            $count = 1;
            do{
            $location = $saveDirectoryPDF . $count . $pdf;
            $count++; 
            }while(is_file($location));

            if (move_uploaded_file($tempName, $location))   //Moves the temp file on server
            {                                                           //to directory with real name
                $pdf = $location;
            } 
            else 
            {

                echo "hi";
                echo '<h1> There was an error while uploading the file.</h1>';
            }
        }
    }else{
        $pdf = "";
    }
    if(isset($_POST['link']) && $_POST['link'] != ""){
        $pdf = $_POST['link'];
    }
    //======================end of pdf upload==============

The output of the line 'print_r($_FILES['pdf']);' is

    Array ( [name] => takeoutmenu.pdf [type] => [tmp_name] => [error] => 1 [size] => 0 )

回答1:


Some providers does not allow you change certain values in running time. Instead of this, try to either change it in the real php.ini file or use an .htaccess (For Apache web servers) where you can add your configuration. You can find more information in the PHP.net article about this subject: How to change configuration settings.

Based on your story, example .htaccess

<IfModule mod_php5.c>
php_value upload_max_filesize 100000000
php_value post_max_size 110000000
php_value memory_limit 120000000
php_value max_input_time 20
</IfModule>


来源:https://stackoverflow.com/questions/8216085/php-file-upload-error-of-1-what-is-correct-way-to-use-php-ini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!