📜  如何在PHP中上传文件?

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

如何在PHP中上传文件?

在本文中,我们将学习如何使用PHP上传文件。让我们先了解一些基本配置。

方法:在您的“PHP.ini”文件中,搜索“file_uploads”参数并将其设置为“On”,如下所述。

file_uploads = On

在“index.html”文件中, enctype必须是 multipart/form-data,方法必须是 POST。

示例1:首先,我们创建了一个HTML文件,以便我们可以通过post方法使用表单上传文件,并且确保该方法是post方法很重要。

在“上传文件。 PHP”,我们先从 post 方法中存储目录名和文件信息,然后我们检查目录的存在,扩展的有效性。我们可以检查文件大小是否小于最大文件大小以及文件是否存在,以便我们在所有验证后上传文件。

允许的扩展名可以根据程序的需要更改或添加到数组中。

index.html


  

    
        Directory
        
        Select image to upload:                  
                


uploading_file.php

 "image/jpg",
                            "jpeg" => "image/jpeg",
                            "gif" => "image/gif",
                            "png" => "image/png");
        $file_name = $_FILES["fileToUpload"]["name"];
        $file_type = $_FILES["fileToUpload"]["type"];
        $file_size = $_FILES["fileToUpload"]["size"];
      
        // Verify file extension
        $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  
        if (!array_key_exists($ext, $allowed_ext)) {
            die("Error: Please select a valid file format.");
        }    
              
        // Verify file size - 2MB max
        $maxsize = 2 * 1024 * 1024;
          
        if ($file_size > $maxsize) {
            die("Error: File size is larger than the allowed limit.");
        }                    
      
        // Verify MYME type of the file
        if (in_array($file_type, $allowed_ext))
        {
            // Check whether file exists before uploading it
            if (file_exists("upload/" . $_FILES["fileToUpload"]["name"])) {
                echo $_FILES["fileToUpload"]["name"]." is already exists.";
            }        
            else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
                  $target_file)) {
                    echo "The file ".  $_FILES["fileToUpload"]["name"]. 
                      " has been uploaded.";
                } 
                else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        }
        else {
            echo "Error: Please try again.";
        }
    }
    else {
        echo "Error: ". $_FILES["fileToUpload"]["error"];
    }
}
?>
  



上传文件。 PHP


 "image/jpg",
                            "jpeg" => "image/jpeg",
                            "gif" => "image/gif",
                            "png" => "image/png");
        $file_name = $_FILES["fileToUpload"]["name"];
        $file_type = $_FILES["fileToUpload"]["type"];
        $file_size = $_FILES["fileToUpload"]["size"];
      
        // Verify file extension
        $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  
        if (!array_key_exists($ext, $allowed_ext)) {
            die("Error: Please select a valid file format.");
        }    
              
        // Verify file size - 2MB max
        $maxsize = 2 * 1024 * 1024;
          
        if ($file_size > $maxsize) {
            die("Error: File size is larger than the allowed limit.");
        }                    
      
        // Verify MYME type of the file
        if (in_array($file_type, $allowed_ext))
        {
            // Check whether file exists before uploading it
            if (file_exists("upload/" . $_FILES["fileToUpload"]["name"])) {
                echo $_FILES["fileToUpload"]["name"]." is already exists.";
            }        
            else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
                  $target_file)) {
                    echo "The file ".  $_FILES["fileToUpload"]["name"]. 
                      " has been uploaded.";
                } 
                else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        }
        else {
            echo "Error: Please try again.";
        }
    }
    else {
        echo "Error: ". $_FILES["fileToUpload"]["error"];
    }
}
?>
  


输出:

输入目录名称并选择文件

在工作文件夹的“uploads”文件夹中,用户可以找到上传的“gfg_sample.png”文件。