2013-01-13 70 views
0

我想結合多個表單元素。我有2個按鈕,1個特TBOX和1個上傳框和1個下拉菜單如下圖所示:Php - 結合多個表單元素

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

這是一個有用的評論 – samayo

+0

是啊,您選擇的權利,我將現在編輯 – vodich

+0

@vodich呃..這樣的工作,但毀了我的登錄腳本.. '警告:require_once(logincheck.php):未能打開流:在第二行的C:\ xampp \ htdocs \ Task2PHP \ login.php中沒有這樣的文件或目錄# '致命錯誤:require_once():無法打開所需'logincheck.php'(include_path ='.; C:\ xampp \ php \ PEAR')在C:\ xampp \ htdocs \ Task2PHP \ login.php在第二行' – Brian