سؤال

I have a PDF uploader that is supposed to save a file to a file path that is based off of a username variable selected from a drop down menu.

Everything works but the uploader, as it displays the usernames and the directories are created upon registration. So there is no issue with that. My issue lies within the code below with the uploader:

<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. if this could be adjusted to upload multiple files at the same time that would be great.

هل كانت مفيدة؟

المحلول

Your conditional statement if(isset($_POST['submit'])) is looking for a submit button named "submit", yet yours is named "upload_pdf".

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

That should either read as if(isset($_POST['upload_pdf'])) or rename your submit button to:

<input type="submit" value="Upload pdf" name="submit" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top