Php - 组合多个表单元素

时间:2013-01-13 13:54:51

标签: php file-upload drop-down-menu directory subdirectory

我正在尝试组合多个表单元素。我有2个按钮,1个tex tbox和1个上传框以及1个下拉菜单,如下所示:

upload.php

因此,用户可以选择要上载的目录,通过单击“创建”按钮和上载文件来创建新目录。但是,使用我当前的代码,它不起作用(文件上传到默认目录)。

这是代码:

<?php
  include("login.php");

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title>FileStore - Upload Files</title>
    <link rel="stylesheet" href="./CSS/style.css" type="text/css" media="screen, projection" />
</head>
<body>
<div id="wrapper">
    <header id="header">
    <div id="header-content">
        <strong><h1>FileStore</h1></strong> Upload multiple files at once!
    </div>
    <div class="login-info" >
<?php
    if ($isLoggedin === false) {
        echo '  <form action="" method="POST">
        Username: <input name="username" > 
        Password: <input type="password" name="password" size="8">
              <input type="submit" name="submit" value="Login">
                </form>';
        echo "<p align='right'>You are not logged in.</p>";
        echo "<b><a href='registration.php'>Register</a></b>";
    }else{  
         echo $welcomeMsg;
    }   
?>
    </div>
    </header><!-- #header-->
    <section id="middle" align="center">
        <div id="container">
        <br><br>
            <div id="content">
                <strong><h1>Upload files</h1></strong><br><br>
                <div id="upload-file" >
                <form action="" method="post" enctype="multipart/form-data">
                <input type="file" value="" name="upload[]" multiple>
                <button type="submit">Upload!</button>
                            </form>
                <select name="myDirs">
                <option value=""  selected="selected">Select a folder</option>
                <?php
                if (chdir("./files/" . $userid)) {
                   $dirs = glob('*', GLOB_ONLYDIR);
                   foreach($dirs as $val){
                      echo '<option value="'.$val.'">'.$val."</option>\n";
                   }        
                    } else {
                   echo 'Changing directory failed.';
                }
                ?>          
<?php
            include("dbConfig.php");
    $Username = $_SESSION["username"];
    global $userid;
    $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'];
    }

    $newDir = $_REQUEST["newDir"];
    $upload_dir  = './files/' . (string)$userid . '/';
    $folder_path = $upload_dir . "/" . $newDir . "/" ;
    if (!file_exists($folder_path)) {
            mkdir($folder_path, 0777, true);
            chmod($folder_path, 0777);  
            echo "Folder" . $newDir . "created.";
            echo "Redirecting to upload page..";
            header( 'Location: ../upload.php' ) ;
        } else {
           echo "Error creating " . $newDir;
        }   
        echo "<form action='' method='post'>";
        echo "<input type='text' name='newDir'> ";      
        echo "<input type='submit' value='Create'>";
        echo "</form>";     
?>      
<?php
        include("dbConfig.php");
        $Username = $_SESSION["username"];
        global $userid;
        global $up_path;

        $error_message[0] = "Unknown problem with upload.";
        $error_message[1] = "Uploaded file too large (load_max_filesize).";
        $error_message[2] = "Uploaded file too large (MAX_FILE_SIZE).";
        $error_message[3] = "File was only partially uploaded.";
        $error_message[4] = "Choose a file to upload.";

        $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'];
        }
        $vpath = strlen($userid);
        $upload_dir  = './files/' . (string)$userid . '/';
        $num_files = count($_FILES['upload']['name']);

        if (!file_exists($upload_dir)) {
            mkdir($upload_dir, 0777, true);
            chmod($upload_dir, 0777);

        } else {

            for ($i=0; $i < $num_files; $i++) {
            $upload_file = $upload_dir . urlencode(basename($_FILES['upload']['name'][$i]));

                if (@is_uploaded_file($_FILES['upload']['tmp_name'][$i])) {
                    if (@move_uploaded_file($_FILES['upload']['tmp_name'][$i], 
                        $upload_file)) {
                        /* Great success... */



                        echo basename($upload_file) . " has been uploaded. <br>";
                        //$content = file_get_contents($upload_file);
                        //echo $content;
                    } else {
                        print $error_message[$_FILES['upload']['error'][$i]];
                    }
                } else {
                    print $error_message[$_FILES['upload']['error'][$i]];
                }    
            }
        }           
?>  
        </div>                              
            </div><!-- #content-->
        </div><!-- #container-->

        <aside id="sideLeft">

            <div id="menu-x" align="center"><br>
            <strong>Menu</strong><br><br>

                    <div class="menu">
                        <ul>
                        <li><a href="index.php">Home</a></li>
                        <li><a href="upload.php">Upload</a></li>
                        <li><a href="files.php">Files</a></li>
                        <li><a href="about.php">About</a></li>
                        <li><a href="help.php">Help</a></li>
                        <li><a href="#">Logout</a></li>
                        </ul>
                        <br style="clear:left"/>
                    </div>

            </div>


        </aside><!-- #sideLeft -->

    </section><!-- #middle-->

    <footer id="footer">
        <strong>FileStore:</strong> A CMT 3315 Project by Brian Livori
    </footer><!-- #footer -->

</div><!-- #wrapper -->

</body>
</html>

我想让用户在上传之前创建一个目录,然后从下拉列表中选择一个选项来选择要上传的目录。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您的表单标记在选择框之前结束,因此将其移到选择框下方,您应该获得与所选上传目录相关的帖子值。