文件上载脚本不使用变量

时间:2013-09-16 00:19:59

标签: php file-upload multifile-uploader

我有一个PDF上传器,它应该将文件保存到基于从下拉菜单中选择的用户名变量的文件路径。

一切正常但上传者,因为它显示用户名和目录是在注册时创建的。所以没有问题。我的问题在于以下代码中的上传者:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="pdf" /><br />
    <select name="folder"> 

<?php
$con=mysqli_connect("host","user","pass","dbname");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT `first_name`, `last_name`, `username` FROM `cw_users` WHERE 1");
$user = 'username';

while($row = mysqli_fetch_array($result)) {
  echo "<option value='". $row["username"] ."'>";
  echo " $row[username] "; 
  echo "</option>";
}

mysqli_close($con);
?>

<?php
if (isset($_POST['submit'])) {
  $pdfDirectory = "Users/".$_POST['folder']."/uploaded/";

  //get the name of the file
  $filename = basename( $_FILES['pdf']['name'], ".pdf");

  //remove all characters from the file name other than letters, numbers, hyphens and        underscores
  $filename = preg_replace("/[^A-Za-z0-9_-]/", "", $filename).".pdf";

  if (move_uploaded_file($_FILES['pdf']['tmp_name'], $pdfDirectory.$filename)) {
    //the path to the PDF file
    $pdfWithPath = $pdfDirectory.$filename;
  }
}

?>

    </select>
    <input type="submit" value="Upload pdf" name="upload_pdf" />
</form>

P.S。如果这可以调整为同时上传多个文件那么好。

1 个答案:

答案 0 :(得分:1)

您的条件语句if(isset($_POST['submit']))正在寻找名为“submit”的提交按钮,但您的名称为“upload_pdf”。

<input type="submit" value="Upload pdf" name="upload_pdf" />
                                        ^^^^^^^^^^^^^^^^^

应该读作if(isset($_POST['upload_pdf']))或将提交按钮重命名为:

<input type="submit" value="Upload pdf" name="submit" />