0

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.

19
  • First this if (isset($_POST['submit'])) { is looking for a submit button named submit and yours is <input type="submit" value="Upload pdf" name="upload_pdf" /> whose name is upload_pdf. If anything that should be if (isset($_POST['upload_pdf'])) { or rename your submit button like this <input type="submit" value="Upload pdf" name="submit" /> - Do that and try again. Commented Sep 16, 2013 at 0:52
  • thank you yeah that did the trick lol i have been coding for about 48 hours non stop cant believe i missed that. Commented Sep 16, 2013 at 1:29
  • You're welcome. I will make it an answer then and you can accept it to mark it as answered. Glad it worked out. Commented Sep 16, 2013 at 1:35
  • Otherwise your question will remain in the "unanswered" category. Commented Sep 16, 2013 at 1:40
  • In order to have multiple files to be uploaded, have a look at this davidwalsh.name/multiple-file-upload which could be useful. You could also look into Uploadify uploadify.com Commented Sep 16, 2013 at 1:49

1 Answer 1

1

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" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.