0

我正在尝试将文件上传到使用 PHP 创建的目录。应用程序应根据用户的 UserID 在根目录内创建一个子目录。(例如文件/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>";
                    }




                 }

            }

目录结构

4

2 回答 2

0

大脑,PHP支持一次上传一个文件,如果表单中有多个文件字段,则应保留 "$dest = ("files/" . $dirname . "{$_FILES['files']['name' ][$key]}");" 在循环。$Key 在循环外将为空。

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

于 2013-01-08T17:18:49.833 回答
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>";
    }

} 

?>

于 2013-01-08T17:27:42.787 回答