📜  编写代码以在PHP中上传文件?

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

编写代码以在PHP中上传文件?

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

在您的“PHP.ini”文件中,搜索 file_uploads 并将其设置为“On”。

file_uploads = On

方法:

  • 首先,我们创建一个 HTML 文件,在其中创建一个表单,以便我们可以使用 POST 方法上传文件。
  • $target_dir变量 在“文件上传。 PHP”通过 post 方法获取目录名称。
  • $target_file获取上传文件的目录位置。
  • $imageFileType用于获取文件扩展名,以便我们确保文件具有有效的扩展名。
  • $extensions用于存储有效的扩展,因此您可以根据程序的要求更改它们,并在扩展数组中添加或删除它们。
  • 在最后一步中,输入您要上传文件的目录名称,然后从本地设备中选择文件并提交。您将看到文件已上传,如下图所示。
index.html


  

  
    
        Directory
        Select image to upload:         
             
     


fileupload.php

 10000000) {
  
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
            exit;
        }
  
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
           echo "Sorry, your file was not uploaded.";
        } 
         else 
        {
  
            // If everything is ok, try to upload file
            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.";
            }
        }    
?>
  



上传文件。 PHP


 10000000) {
  
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
            exit;
        }
  
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
           echo "Sorry, your file was not uploaded.";
        } 
         else 
        {
  
            // If everything is ok, try to upload file
            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.";
            }
        }    
?>
  


输出:

写入目录名称并选择文件

上传文件后