1

所以我在我的网站上有一个文件上传部分,用户可以上传任何 doc 或 docx 文件夹。这是我的html代码:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Select a file: <input type="file" name="upload">
<input type="submit">

这是 upload_file.php 的代码:

<?php
    session_start();
    $allowedExts = array("doc", "docx");
    $extension = end(explode(".", $_FILES["upload"]["name"]));

    if (($_FILES["upload"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["upload"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
            echo "Type: " . $_FILES["upload"]["type"] . "<br />";
            echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

            if (file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]))
            {
                echo $_FILES["upload"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["upload"]["tmp_name"],
                "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
                echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }
?>

但这不会上传任何内容,即使它回复了一条成功的消息。我在想这是因为它不允许我创建目录。上面的代码有什么问题,还是我必须添加更多代码才能创建一个目录。如果有帮助的话,这里是我的文件夹结构:

文件夹

4

3 回答 3

14

您必须创建您尝试将文件移动到的目录,它不会由 move_uploaded_file 自动创建。

使用 mkdir()、http ://php.net/mkdir 创建目录,然后移动文件。

这是脚本的另一种结尾,应该可以

// Create directory if it does not exist
if(!is_dir("Proposals/". $_SESSION["FirstName"] ."/")) {
    mkdir("Proposals/". $_SESSION["FirstName"] ."/");
}

// Move the uploaded file
move_uploaded_file($_FILES["upload"]["tmp_name"], "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);

// Output location
echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
于 2012-10-02T23:43:28.163 回答
2

您需要检查目录是否存在,如果不存在,则创建它。

if (!file_exists("Proposals/". $_SESSION["FirstName"])) {
      mkdir("Proposals/". $_SESSION["FirstName"]);
}
于 2012-10-02T23:47:00.297 回答
0

您正在将文件上传到不存在的目录,因此您需要先创建它,您的 upload_file.php 应该像

<?php
session_start();
$allowedExts = array("doc", "docx");
$extension = end(explode(".", $_FILES["upload"]["name"]));

if (($_FILES["upload"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
    if ($_FILES["upload"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
    }
    else
    {
        echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
        echo "Type: " . $_FILES["upload"]["type"] . "<br />";
        echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

        if (file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]))
        {
            echo $_FILES["upload"]["name"] . " already exists. ";
        }
        else
        {
            // Check if directory exists if not create it 
            if(!is_dir("Proposals/". $_SESSION["FirstName"] ."/")) {
               mkdir("Proposals/". $_SESSION["FirstName"] ."/");
             }
             move_uploaded_file($_FILES["upload"]["tmp_name"],
            "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
            echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
        }
    }
} else {
    echo "Invalid file";
}
?>
于 2016-01-27T21:53:51.340 回答