php 文件上传错误 1 ​​- 使用 php_ini 的正确方法是什么?

发布于 2024-12-17 10:04:00 字数 2564 浏览 1 评论 0原文

我在将 pdf 上传到服务器时遇到问题。 upload_max_filesize 为 2M,文件大于此值,约为 4M。我在这里发现了一个与我的问题类似的帖子

$_FILE 上传大文件给出错误 1,即使 upload_max_size 大于文件大小

我可以从 php.net 收集到的 ini_set 命令的正确用法是这样的,我目前正在使用。

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

但在我发布的链接中,他们似乎使用了不同的方法(如果他们不只是总结正确的代码)。但我的代码似乎也不能按原样工作。我有 在我的页面底部,它说 upload_max_filesize 仍然是 2M。我使用的 ini_set 语法是否正确?或者我上传 pdf 时遇到其他问题?

我处理上传的代码是

    //======================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==============

'print_r($_FILES['pdf']);' 行的输出是

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

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-12-24 10:04:00

某些提供程序不允许您在运行时更改某些值。相反,尝试在真实的 php.ini 文件中更改它或使用 .htaccess(对于 Apache Web 服务器),您可以在其中添加配置。您可以在 PHP.net 文章中找到有关此主题的更多信息:如何更改配置设置< /a>.

根据您的故事,例如 .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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文