【问题标题】:Identifying a pdf file and upload it to folder识别pdf文件并将其上传到文件夹
【发布时间】:2015-08-29 09:38:14
【问题描述】:

我正在使用此 php 代码将图像上传到文件夹,但我也想允许上传 pdf 文件,所以我修改了一些代码:

<?php

    $target_dir = "extra_images/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $textFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
             //echo "<div class=\"alert alert-success\" role=\"alert\"><strong><span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\"></span> Correct image type.</strong></div>";
                    $uploadOk = 1;
                } else {
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File is not an image.</strong></div>";
                    $uploadOk = 0;
                }
            }
            // Check if file already exists
            if (file_exists($target_file)) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 3750000) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $textFileType != "pdf" ) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Only jpg, jpeg, png, gif and pdf (for the Plan Article) files are allowed.</strong></div>";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

                    echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form.";
                } else {
                    echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
                }
    }
    echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
 ?>

我添加了这一点:

&& $textFileType != "pdf" and this: $textFileType = pathinfo($target_file,PATHINFO_EXTENSION);

但我所做的这些更改不起作用,它仍然返回 "this is not an image" 消息。

代码的哪一部分标识了文件类型? $imageFileType 是 php 用来识别文件类型的特殊变量吗?

我真的很困惑。有人可以帮忙吗?

【问题讨论】:

  • 问题是,它是否在您添加新行之前就抛出了该错误?
  • 这里的问题是,您需要对整行使用 OR || 运算符,而不是 AND &amp;&amp; 运算符。您是在告诉 PHP 检查上传的文件是否为 JPG AND PNG AND GIF AND PDF。
  • 不,脚本工作者适用于图像类型文件。我在这里得到它:w3schools.com/php/php_file_upload.asp
  • 您在下面得到了答案。问他们
  • 并让那个人知道它是否有效,在答案下。

标签: php pdf mime identify


【解决方案1】:

如果你想检查扩展名,pdfs的文件类型是application/pdf

但是,虽然您可以检查文件扩展名,但这并不是识别文件是否为 pdf 的一种非常可靠的方法(几乎可以轻松更改任何文件的文件扩展名,从而造成巨大的安全漏洞)。

虽然 php 中没有像 getimagesize() 这样的 pdf 文件,但您仍然可以检查 mime 类型,这是该过程中相当不错的一步,如下所示:

    if (!empty($_FILES['fileToUpload']['tmp_name'])) {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
            if ($mime != 'application/pdf') {

                echo 'this is not a PDF file!';
                exit();
            }

【讨论】:

  • 感谢您的回复。我试过你的代码,但我不能让它工作,我是一个 php 初学者......可能我会寻找一段代码,它对我想允许的所有文件格式进行 mime 验证。但现在只想让这个工作,因为上传页面只被非常有限的人使用。
  • 这可能与$_FILES['article_pdf']有关,如果你没有改变的话。 @FredericoLopes
  • @Fred-ii- 好地方。我从我自己的一些代码中复制了这个。错过了一个变量
  • @nomistic 让我们看看 OP 的风向是否会改变 ;-)
  • @FredericoLopes 请注意,正如我提到的,getimagesize() 不适用于 pdf 文件,并且上面的代码将适用于所有文件类型。我只是给了你一个 pdf 的,因为那是你要求的。
【解决方案2】:

感谢大家的帮助,这里经过一些代码角力,是最终的功能版本:

<?php

            $target_dir = "extra_images/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

            // Check if image file is a actual image or fake image
            /*if(isset($_POST["submit"])) {
                $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
                if($check !== false) {
                    //echo "<div class=\"alert alert-success\" role=\"alert\"><strong><span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\"></span> Correct image type.</strong></div>";
                    $uploadOk = 1;
                } else {
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File is not an image.</strong></div>";
                    $uploadOk = 0;
                }
            }*/
            // Check if file already exists
            if (file_exists($target_file)) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 3750000) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
                $uploadOk = 0;
            }

            // Allow certain file formats
            /*if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Only jpg, jpeg, png, gif and pdf (for the Plan Article) files are allowed.</strong></div>";
                $uploadOk = 0;
            }*/

            //Check for pdf format
            if (!empty($_FILES['fileToUpload']['tmp_name'])) {
                $finfo = finfo_open(FILEINFO_MIME_TYPE);
                $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']);
                if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) {

                    $uploadOk = 0;
                    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>";

                    //exit();

                }} //this bracket was missing I think



            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

                    echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form.";
                } else {
                    echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
                }
            }
            echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";

?>

关于 && / ||问题,起初我也有使用 || 的想法运算符,我试过了,但没有用,可能是因为我们使用 != 进行比较,所以如果某个文件不是 jpg,不是 pdf... = 0 所以不会上传。

看看代码,这是写的,但仍然违背我的逻辑:)

非常感谢您的帮助;)

【讨论】:

  • 干得好。 w3schools 网站(这是一个很好的起点)没有涵盖的一件事是 php 本身的安全漏洞。 PHP 是弱类型的,这意味着它会在出现许多错误后继续在后台运行(尤其是您看到的那些软错误,例如“通知”或“警告”)。我发现在出现错误的任何时候用exit(); 终止脚本是个好主意,以防万一。我建议查看此内容:owasp.org/index.php/PHP_Security_Cheat_Sheet
  • 谢谢!我曾经知道 Pascal :) 和 C 但是这些语言,如 Java 和 PHP 让我有点困惑,考虑到的东西太多了,我很少能自己解决问题,但它越来越好 ;) 你认为我可以杀人吗最后的脚本,在结束 php 标记之前?该脚本将在 iframe 中运行,因此我们的想法是让用户可以根据需要将其上传到那里。
  • 我的意思是在错误之后,像这些:$uploadOk = 0; 这是个人喜好,但我一般喜欢生成错误,然后用exit(); 停止脚本不确定它有多大的不同(我不是王牌;我自己还在这里学习),但它让我感觉更安全。如果有人需要上传更多文件,脚本可以重新启动。
  • 好的,我尝试将退出函数保留在您将其放置在代码中的位置,但随后它不会显示按钮,我可以将按钮放在每个错误消息中,在回显之后和退出之前( ) 或使用按钮创建一个函数并在每次 exit() 之前调用它...我将对此进行调查。
  • 不用担心,只是个人喜好。您拥有的代码大部分都很好。如果我希望代码完全停止,我只会使用它。如果需要显示按钮,则需要在按钮后放置一行
猜你喜欢
  • 2021-01-09
  • 2016-10-11
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多