PHP - 创建并上传到目录

时间:2013-01-08 17:02:58

标签: php mysql session mkdir subdirectory

我正在尝试将文件上传到使用PHP创建的目录。应用程序应根据用户的UserID在根目录中创建子目录。 (例如files / 14 /)。

正在创建目录,但文件未上传到子目录。

这是代码:

<?php 


                include("dbConfig.php");

                $Username = $_SESSION["username"];

                global $userid;

                $Password = $_SESSION["password"];

                $Password = md5($Password);

                $sql = "SELECT UserID FROM users WHERE Username = '".$Username."'";

                $result = mysql_query($sql) or die(mysql_error());

                while($row = mysql_fetch_assoc($result)) {

                        $userid = $row['UserID'];

                }


                echo $userid;

                $dirname = (string)$userid;

                $filename = ("/folder/" . "$dirname" . "/");

                if (!file_exists($filename))

                {

                            mkdir("files/$dirname", 0777);

                            if (isset($_FILES['files'])) {

                            echo "<div id='files_table'><table class='center'.><tr><td>";

                            $dest = ("files/" . $dirname . "{$_FILES['files']['name'][$key]}");

                            foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){

                            move_uploaded_file($tmp_name, $dest );

                            echo $_FILES['files']['name'][$key], " uploaded.", "<br>";

                            }

                 }else {


                        if (isset($_FILES['files'])) {

                        echo "<div id='files_table'><table class='center'.><tr><td>";

                            $dest = ("files/" . $dirname . "{$_FILES['files']['name'][$key]}");

                            foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){

                            move_uploaded_file($tmp_name, $dest );

                            echo $_FILES['files']['name'][$key], " uploaded.", "<br>";


                    }
                    echo "</td></tr></table></div><br><br>";
                    }




                 }

            }

Directory Structure

2 个答案:

答案 0 :(得分:0)

脑, PHP一次支持一个文件上传,如果你的表单中有很多文件字段,你应该保留“$ dest =(”files /“。$ dirname。”{$ _FILES ['files'] ['name'] [ $关键]} “);”在循环。 $ key将在循环外空。

循环不能在temp_name上,temp_name是php要上传的文件的临时副本。

答案 1 :(得分:0)

以下是修改后的代码:

注意:我也纠正了代码中的一些错误。请比较。

<?php 
include("dbConfig.php");
$Username = $_SESSION["username"];
global $userid;
$Password = $_SESSION["password"];
$Password = md5($Password);

$sql = "SELECT UserID FROM users WHERE Username = '".$Username."'";

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   $userid = $row['UserID'];
}


$dirname = (string)$userid;
$filename = ("$dirname" . "/");
if (!file_exists($filename)) {
    mkdir("files/$dirname", 0775);
}

if (isset($_FILES['files'])) {
    echo "<div id='files_table'><table class='center'.><tr><td>";        
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
        $dest = ("files/" . $dirname . "{$_FILES['files']['name'][$key]}");
        move_uploaded_file($tmp_name, $dest );
        echo $_FILES['files']['name'][$key], " uploaded.", "<br>";
    }

} 

&GT;