php 文件大小验证

php file size validation

我想在 php 中为每个文件创建文件大小验证。我使用 for 循环添加附件,然后创建条件来检查文件,但它不起作用。没有错误,但它发送了带有超大附件的邮件,而不是停止。没有大小验证部分,它发送邮件没有任何问题。

为了发送,我使用了 php 邮件程序。


   <?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
$honeypot = $_POST['honey'];
$user_name = $_POST['name'];
$user_email = $_POST['email'];
$user_message = $_POST['message'];
$user_phone = $_POST['phone'];
$honeypot = trim($_POST["honey"]);
$max_size = 2 * 1024 * 1204; //2mb
$attachment = $_FILES['uploaded-file'];

if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if(!empty($honeypot)) {
        echo "NO SPAM!"; 
        exit;
      }    else {

        $mail = new PHPMailer; //From email address and name 
        $mail->isMail(); 
        $mail->From = $user_email;
        $mail->addAddress("jaroslaw.mor@gmail.com");
        $mail->isHTML(true); 
        $mail->Subject = "Zapytanie ze strony www"; 
        $mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
        $mail->AltBody = "Telefon:$user_phone\n$content"; 
        
        
        if(isset($attachment)) {

            for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
                if ($_FILES['uploaded-file']['error'] !== UPLOAD_ERR_OK) continue;
                $file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i]; 
                $file_name = $_FILES['uploaded-file']["name"][$i];  

                if($_FILES['uploaded-file']["name"][$i];  > $max_size) {
                    echo "file is too big";
                    die();
}
                 
                else{
                 move_uploaded_file($fileTmpName,  "uploads/" . $filename);
                 $mail-> AddAttachment("uploads/". $filename);
                                                                }      
            }//for
        }//isset

        if(!$mail->send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
           exit();
      } 
      
              else {
              header("Location: sent.html");
                             
                 exit();

      }//if send else

}//honey else end

}//post end

我稍微重建了我的代码,也遵守了comments/clues。

但是还是一样的问题

我在这里看到的是您在检查文件大小之前使用了“move_upload_file”方式。这意味着文件将在检查文件是否更大之前被上传。应该是这样的;

if( $_FILES['uploaded-file']["name"]['size'][$i] > $maxsize ) {

            echo "file is too big";
            die();
        }else{
         move_uploaded_file($fileTmpName,  "uploads/" . $filename);
         $mail-> AddAttachment("uploads/". $filename);
}

这是复制粘贴错误

$_FILES['uploaded-file']["name"]['size'][$i]

根据 PHP documentation,尺寸小于 $_FILES['uploaded-file'] 不小于 $_FILES['uploaded-file']['name']。见@yuxufabio 的评论。

而且 if (isset($attachments)) 检查有点奇怪。它总是 return 一个数组(意思是 true),即使它是空的。您可能的意思是,是否设置了表单变量本身

if (isset($_FILES['uploaded-file'])) {
for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) { 

最后,您应该检查是否有错误,因为上传不能保证在客户端的错误下工作,例如空文件或网络错误

if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
...
if ($_FILES['uploaded-file']['size'][$i] > $maxsize) {
...