在php中上传带有POST请求的文件

时间:2015-02-09 18:09:10

标签: php upload

我在将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.";
    }
?>

1 个答案:

答案 0 :(得分:0)

只是为了确定,但你的主人有一个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