PHP文件上传创建目录 [英] PHP File Upload Creating Directory

查看:126
本文介绍了PHP文件上传创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have a file upload portion on my website where the user can upload any doc or docx folder. Heres my html code:

<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的代码:

And here's the code for 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";
    }
?>

但是即使通过成功的邮件回复,这也不会上传任何东西。我在想,这是因为它不会让我创建一个目录。上面的代码有什么问题,还是要添加一些更多的代码来创建一个目录。这里我的文件夹结构,如果它有帮助:

But this won't upload anything even though it replied back with with a successful message. I'm thinking it's because it won't let me create a directory. Is there anything wrong with the above code or do I have to add some more code to make it create a directory. Here my folder structure if it helps at all:

推荐答案

您必须创建要将文件移动到的目录,不会自动创建通过move_uploaded_file。

You have to create the directory you're trying to move the file to, it won't automatically get created by move_uploaded_file.

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

Use mkdir(), http://php.net/mkdir, to create the directory and then move the file.

这是一个替代脚本的结尾,应该这样做

Here's an alternative ending to your script, which should do

// 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"];

这篇关于PHP文件上传创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆