在 php 中使用 POST 请求上传文件

Upload a file with a POST request in php

我在将 zip 文件上传到我的服务器时遇到问题。 move_uploaded_file 函数每次都失败。我不明白这个问题。文件夹权限为777,文件大小约为2 Mb。

<html>
  <body>

    <form action="../API/upload_zip.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Zip" name="submit">
    </form>

  </body>
</html>

-

<?php
    $target_dir = "../uploads/";
    $machineID="H725";
    $path=$target_dir.$machineID;
    if (!file_exists($path)) 
    {
        if(!mkdir($path, 0777, true)) die('Failed to create folders 1...');

        chmod($path, 0777);
    }      

    $pathWithData= $path."/".date("Y_m_d_h_i");
    if (!file_exists($pathWithData)) 
    {
        if(!mkdir($pathWithData, 0777, true)) die('Failed to create folders 2...');

        chmod($pathWithData, 0777);
    } 

    $final_path = $pathWithData ."/". basename($_FILES["fileToUpload"]["name"]);
    echo "PATH: ".$final_path."<br>";


    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $final_path))
    {
        echo "<br>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    }
    else 
    {
        echo "<br>Sorry, there was an error uploading your file.";
    }
?>

只是为了确定,但您的主机有一个 file_uploads = On,对吧?检查 php.ini 或创建 phpinfo();

此外,尝试用 var_dump($_FILES); 回显您的 $_FILES 如果上传没有问题,[error] 应该为 0。这些是返回的错误常量:

UPLOAD_ERR_OK: 0
UPLOAD_ERR_INI_SIZE: 1
UPLOAD_ERR_FORM_SIZE: 2
UPLOAD_ERR_NO_TMP_DIR: 6
UPLOAD_ERR_CANT_WRITE: 7
UPLOAD_ERR_EXTENSION: 8
UPLOAD_ERR_PARTIAL: 3

在 php.ini 中,更改这些值:

upload_max_filesize = 100M
post_max_size = 100M