0

我有一个 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>

PS如果这可以调整为同时上传多个文件,那就太好了。

4

1 回答 1

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" />
于 2013-09-16T01:41:03.957 回答