PHP文件上传到用户文件夹

时间:2015-07-16 12:30:13

标签: php

好的,这是我的脚本,它注册用户,并在注册成功后,在用户文件夹中创建用户子文件夹(基于POST_ed用户名)。有2个脚本:register.php和addfile.php。事情是,脚本将文件上传到用户文件夹,而不是创建用户子文件夹。有任何想法如何修改脚本以便将文件上传到用户子文件夹?谢谢。

这是register.php:

session_start();

    if(isset($_SESSION['user']) != '') {
        header ("Location: home.php");
    }

    require_once ('dbconnect.php');

        if(isset($_POST['btn-signup']))
    {
        $uname = mysql_real_escape_string($_POST['uname']);
        $email = mysql_real_escape_string($_POST['email']);
        $upass = mysql_real_escape_string($_POST['pass']);

        $sql = mysql_query ("SELECT username FROM users WHERE username='".$uname."'");
        $num = mysql_num_rows($sql);

            if ($num > 0)  { 
                ?>
                    <script>alert('Duplicate username');</script>
                <?php

            }
                else {
                    if(mysql_query("INSERT INTO users(username,email,password) VALUES('$uname','$email','$upass')")) {
                        ?>
                            <script>alert('Successfully registered');</script>
                        <?php

                        $_SERVER['QUERY_STRING'];
                        $foldername = $_POST['uname'];
                        $path = "users/";
                        $complete = $path.$foldername;

                        if (mkdir($complete, 0770)) {
                            ?>
                                <script>alert('Folder <?php echo "\"$foldername\""; ?> has been created');</script>
                            <?php
                        }
                            else {
                                ?>
                                    <script>alert(Folder not created);</script>
                                <?php
                            }               
                    }

                }   
    }

这是(addfile.php):

$folder = "users/";
$filename = $_FILES['filep']['name'];
$filedir = $folder.$filename;

if ($_POST['action'] == "Load") {

    if (file_exists($filedir)) {
        ?>
            <script>alert('File" <?php echo $_FILES["filep"]["name"] . "already exists."; ?>');</script>
        <?php
    }
        elseif (move_uploaded_file($_FILES['filep']['tmp_name'], $filedir)) {

            ?>
                <script>alert('File "<?php echo "$filename"; ?> "loaded');</script>
            <?php               
        }
            else {
                ?>
                    <script>alert('File not loaded');</script>
                <?php
            }
}

1 个答案:

答案 0 :(得分:0)

你的addfile.php中缺少$ _SESSION ['user'](上传文件仅供用户使用,所以你需要让你的会话以填充的$ _SESSION ['user']开始,如果不是你的观念问题)

$filedir = $folder.$_SESSION['user'].'/'.$filename;

现在在'成功注册'警告之前的register.php中:

$_SESSION['user'] = $_POST['uname'];

并且不要忘记

session_start();

并关闭数据库连接