【问题标题】:Testing error with file upload If statements in PHPPHP中的文件上传If语句测试错误
【发布时间】:2019-02-24 18:24:29
【问题描述】:

这是我在 Stack Overflow 上的第一篇文章,所以请耐心等待我 - 我已经求助于发布,因为我无法通过 Google/Stack Overflow 找到答案。

我是 PHP 新手,正在学习如何添加文件上传。我有一个非常基本的 html 表单,它指向一个 PHP 页面。

            <form action="UploadPage.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="MAX_FILE_SIZE" value="100000"/>
            <input type="file" name="uploadedXML"/>
            <input type="submit" value="Upload"/>

处理文件上传的 PHP 代码有一系列 if 语句来检查文件是否是正确的类型大小等。如果有错误,则会在错误页面上生成相应的错误消息。

我一直在测试上传各种文件类型以确定错误语句是否正确发生,我在第二个(检查文件类型)和第三个(检查文件大小)方面遇到了问题。

如果文件类型检查 if 语句首先出现,我发现如果我上传大于最大大小 (100kb) 的 XML 文件,我仍然会收到与文件类型检查有关的错误消息 - 当我应该得到与文件大小有关的错误消息。

但是,如果我交换 IF 语句以便文件大小检查在文件类型检查之前进行,如果我上传不正确的文件类型但大小合适(例如小图像),我会收到与文件有关的错误消息太大了,当我期待与文件类型有关的文件不正确时。

<?php

        const UploadKey = 'uploadedXML';
        const AllowedTypes = ['application/xml','text/xml'];

session_start();


/*Checking for errors*/

if (empty($_FILES[UploadKey]['name'])){//check file actually been uploaded
    header("Location: ErrorPage.php"); 
    $_SESSION['errorMessage']="You forgot to add your file!";
    die();
}

if (!in_array($_FILES[UploadKey]['type'],AllowedTypes)){//Check correct type of file
    header("Location: ErrorPage.php"); 
    $_SESSION['errorMessage']="We only accept XML files I'm afraid";
    die();
    }


if ($_FILES[UploadKey]['error'] == 2) {//Check if size too big
header("Location: ErrorPage.php"); 
       $_SESSION['errorMessage']="Your file is too big for us to handle, awkward! Please choose a file under 100KB.";
        die();
       }

$tempFileLoc = $_FILES[UploadKey]['tmp_name'];
$destFileLoc = 'Uploads/'.$_FILES[UploadKey]['name'];

if (file_exists($destFileLoc)) {// Check if file already exists
    header("Location: ErrorPage.php"); 
    $_SESSION['errorMessage']="We've already got this file, thanks though";
    die();
    }

if ($_FILES[UploadKey]['error']>0){
    header("Location: ErrorPage.php"); 
    $_SESSION['errorMessage']="Unfortunately there's been an error with the uploading process";
    die();
    }

如果您需要查看我的更多代码来帮助回答,请告诉我。

提前非常感谢!

【问题讨论】:

  • 每次检查后,它会die();,这将阻止它进行任何进一步的检查。
  • 同意,但对于大型 (>100kb) XML 文件,它肯定会通过前两个错误语句并在第三个错误语句中死去(文件大小太大')实际发生的事情是它在第二个语句中死掉,即使它不应该应用,因为文件类型是正确的。

标签: php forms file upload


【解决方案1】:

最佳做法是建立一个错误数组,如果它为空则继续下一步,否则返回错误。你可以试试这样的。在您的代码中,您覆盖了错误消息,因此您只看到最后一个应用的消息,而不是所有可能触发的上传消息。

<?php

        const UploadKey = 'uploadedXML';
        const AllowedTypes = ['application/xml','text/xml'];
        $errors = array();

        session_start();


        /*Checking for errors*/
        if (empty($_FILES[UploadKey]['name'])){//check file actually been uploaded
            $errors[] = "You forgot to add your file!";
        }

        if (!in_array($_FILES[UploadKey]['type'],AllowedTypes)){//Check correct type of file
            $errors[] ="We only accept XML files I'm afraid";
         }


        if ($_FILES[UploadKey]['error'] == 2) {//Check if size too big
            $errors[] ="Your file is too big for us to handle, awkward! Please choose a file under 100KB.";      
         }

        $tempFileLoc = $_FILES[UploadKey]['tmp_name'];
        $destFileLoc = 'Uploads/'.$_FILES[UploadKey]['name'];

        if (file_exists($destFileLoc)) {// Check if file already exists
            $errors[] ="We've already got this file, thanks though";
        }

        if ($_FILES[UploadKey]['error'] > 0){
            $errors[] = "Unfortunately there's been an error with the uploading process";
        }

        //if errors were found  
        if(!empty($error)){
            header("Location: ErrorPage.php"); 
            //beware this is now an array and not a single string
            $_SESSION['errorMessage']= $errors;
            die();
        }

【讨论】:

    【解决方案2】:

    该问题是由您在 HTML 表单中包含的 MAX_FILE_SIZE 引起的。

    如果您上传的文件超出了表单中设置的MAX_FILE_SIZE,PHP 会自动清空tmp_nametype,并将文件($_FILES) 的size 设置为0。

    所以$_FILES[UploadKey]['type'] 为空,因此用于检查文件类型是否允许的条件将返回 false。

    要纠正这个问题,您还应该检查以确保类型不为空 if (!empty($_FILES[UploadKey]['type']) && !in_array($_FILES[UploadKey]['type'],AllowedTypes)

    类似这样的:

    <?php
    
      if (!empty($_FILES[UploadKey]['type']) && !in_array($_FILES[UploadKey]['type'],AllowedTypes)){// Make sure the file type is not empty
        header("Location: ErrorPage.php"); 
        $_SESSION['errorMessage']="We only accept XML files I'm afraid";
        die();
      }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2019-01-15
      相关资源
      最近更新 更多