使用 PHP 上传 PDF 文件的问题

发布于 2025-01-06 04:27:43 字数 1432 浏览 1 评论 0原文

我的脚本遇到了一个奇怪的问题,我正在测试该脚本上传 PDF 文件。我可以成功上传一些 pdf 文件,而不能成功上传其他文件,即使它们都是 pdf 文件并且扩展名为 .pdf。 任何人都可以对此进行一些说明

在查看我的代码HTML 部分:

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">                            
    <input type="file"  name="upload" /><br />        
    <input type="submit" name="submit">  

PHP 部分:

  if(isset($_POST['submit'])){
                $output_form = 0;                

                if (($_FILES["upload"]["type"] == "application/pdf")
                    && ($_FILES["upload"]["size"] < 80000)){
                        if (file_exists("upload/" . $_FILES["upload"]["name"]))
                          {
                            echo $_FILES["upload"]["name"] . " already exists. ";
                          }
                          else
                          {
                            move_uploaded_file($_FILES["upload"]["tmp_name"],
                            "upload/" . $_FILES["upload"]["name"]);
                            echo "Stored in: " . "upload/" . $_FILES["upload"]["name"];
                          }                          
                    }else{
                        echo 'Invalid File';
                    }                      
            }

对于某些文件,我得到存储在输出中的输出后, 。对于其他人,我收到消息“无效文件”。

谢谢

I am encountering a strange problem with my script which I am testing to upload PDF files. I can sucessfully upload some pdf files while not the other files, even though they are all pdfs and have .pdf as extension. Can anyone throw some light on this after going thtough my code

HTML PART:

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">                            
    <input type="file"  name="upload" /><br />        
    <input type="submit" name="submit">  

PHP PART:

  if(isset($_POST['submit'])){
                $output_form = 0;                

                if (($_FILES["upload"]["type"] == "application/pdf")
                    && ($_FILES["upload"]["size"] < 80000)){
                        if (file_exists("upload/" . $_FILES["upload"]["name"]))
                          {
                            echo $_FILES["upload"]["name"] . " already exists. ";
                          }
                          else
                          {
                            move_uploaded_file($_FILES["upload"]["tmp_name"],
                            "upload/" . $_FILES["upload"]["name"]);
                            echo "Stored in: " . "upload/" . $_FILES["upload"]["name"];
                          }                          
                    }else{
                        echo 'Invalid File';
                    }                      
            }

For some files I am getting the output, stored in output. For the others I am getting the message 'Invalid File'.

Thanks

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

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

发布评论

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

评论(2

忘你却要生生世世 2025-01-13 04:27:43

您上面的代码似乎有一个条件,如果文件大小大于 80000 那么它应该抛出“无效文件”错误?失败的有多大?我愿意打赌,如果你注释掉这个条件它就会起作用

your code above seems to have a condition that if the filesize is greater than 80000 then it should throw the 'Invalid file' error? What size are the ones that fail? I'd be willing to bet if you comment out that condition it'll work

も让我眼熟你 2025-01-13 04:27:43

有同样的问题。
发现文件类型也可能是application/x-octet-stream
因此,您需要在检查文件大小的同一语句中检查这一点。
像这样的东西:
if (($_FILES['pdfUpload']['type'] == "应用程序/pdf")
|| ($_FILES['pdfUpload']['type'] == "application/x-octet-stream")
&& ($_FILES['pdfUpload']['size'] < 9000000)) //大得多,我们在传输过程中超时

我的 2 美分值

Had the same issues.
Found that the file type could also be application/x-octet-stream
So you need to check for that in the same statement that you are checking the file size.
Something like this:
if (($_FILES['pdfUpload']['type'] == "application/pdf")
|| ($_FILES['pdfUpload']['type'] == "application/x-octet-stream")
&& ($_FILES['pdfUpload']['size'] < 9000000)) //Much larger and we get a timeout during transfer

My 2 cents worth

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