📜  如何在PHP中访问与文件上传相关的错误代码?

📅  最后修改于: 2022-05-13 01:56:15.083000             🧑  作者: Mango

如何在PHP中访问与文件上传相关的错误代码?

当我们没有相应地处理这些情况时, PHP中的错误就会发生。

在文件上传过程中发生了许多错误。错误如下

  • 文件大小大于预期大小
  • 扩展名不正确,或者我们可以根据条件说文件类型不匹配。
  • 目录丢失。
  • 该文件已存在于首选目录中。

我们可以通过内置变量$_FILES[“file”][“error”]访问错误代码,它为我们提供错误代码,它们的定义如下。

  • 0 => '文件上传成功
  • 1 => 上传文件超过限制
  • 2 => 上传的文件超过了 HTML 表单中提到的限制。
  • 3 => 文件部分上传或上传之间有任何错误。
  • 4 => 没有上传文件。
  • 6 => 缺少一个临时文件夹。
  • 7 => 无法将文件写入磁盘。
  • 8 => 一个PHP扩展停止了上传过程。

现在我们通过下面的例子来理解这一点。

示例 1:在此示例中,列出了许多错误。它从本地设备获取目录名和文件,然后通过 POST 方法发送到“file.txt”。 PHP”。然后它检查目录创建,如果没有,则创建一个,否则进行扩展检查并检查数组中列出的允许扩展名(您可以根据程序要求添加)。然后它检查文件的大小,然后检查文件是否存在。



file.php

Error: Specify the directory name...";
          $flag = 0;
          exit;    
       }    
      
    // Check if file was uploaded without errors
    if(isset($_FILES["file"]) && $_FILES["file"]["error"] == 0)
    {
        $allowed_ext = array("jpg" => "image/jpg",
                            "jpeg" => "image/jpeg",
                            "png" => "image/png");
      
        // Verify file extension
        $ext = pathinfo($name, PATHINFO_EXTENSION);
  
        if (!array_key_exists($ext, $allowed_ext))    
        {
            die("Error: Please select a valid file format.");
        }    
              
        $maxsize = 200000;
          
        if ($size > $maxsize)    
        {
            die("Error: ". $_FILES["file"]["error"] . 
            "  File size is larger than the allowed limit.");
        }    
                      
      
        // Verify MYME type of the file
        if (in_array($type, $allowed_ext))
        {
            // Check whether file exists before uploading it
            if (file_exists("$target_dir/".$_FILES["file"]["name"]))    
            {
                echo "".$_FILES["file"]["name"]." is already exists.";
            }        
            else
            {
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
                      
                    echo "The ".  $_FILES["file"]["name"]. " has been uploaded.";
  
                } 
                else 
                {
                    echo "Error : ". $_FILES["file"]["error"] .
                    " Sorry, there was an error uploading your file.";
                }
            }
        }
        else
        {
            echo "Error: ". $_FILES["file"]["error"] . " Please try again.";
        }
    }
    else
    {
        echo "Error: ". $_FILES["file"]["error"] . " File is not uploaded";
    }
  }
?>



文件。 PHP


Error: Specify the directory name...";
          $flag = 0;
          exit;    
       }    
      
    // Check if file was uploaded without errors
    if(isset($_FILES["file"]) && $_FILES["file"]["error"] == 0)
    {
        $allowed_ext = array("jpg" => "image/jpg",
                            "jpeg" => "image/jpeg",
                            "png" => "image/png");
      
        // Verify file extension
        $ext = pathinfo($name, PATHINFO_EXTENSION);
  
        if (!array_key_exists($ext, $allowed_ext))    
        {
            die("Error: Please select a valid file format.");
        }    
              
        $maxsize = 200000;
          
        if ($size > $maxsize)    
        {
            die("Error: ". $_FILES["file"]["error"] . 
            "  File size is larger than the allowed limit.");
        }    
                      
      
        // Verify MYME type of the file
        if (in_array($type, $allowed_ext))
        {
            // Check whether file exists before uploading it
            if (file_exists("$target_dir/".$_FILES["file"]["name"]))    
            {
                echo "".$_FILES["file"]["name"]." is already exists.";
            }        
            else
            {
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
                      
                    echo "The ".  $_FILES["file"]["name"]. " has been uploaded.";
  
                } 
                else 
                {
                    echo "Error : ". $_FILES["file"]["error"] .
                    " Sorry, there was an error uploading your file.";
                }
            }
        }
        else
        {
            echo "Error: ". $_FILES["file"]["error"] . " Please try again.";
        }
    }
    else
    {
        echo "Error: ". $_FILES["file"]["error"] . " File is not uploaded";
    }
  }
?>


输出: