title: Saving a PDF file using PHP
body: <p>I have a problem with saving PDF files to folders on my server. The code worked at one time and now it doesn't. What I want it to do is to check if someone is trying to upload a PDF when a form is submitted, and if there is a PDF in the file field it uploads it and then saves the path to the mysql database. Code is below:</p>
  
  <pre><code>    if (!empty($_FILES['pdf'])){
            $idir = "../files/PDF/"; //my directory file is supposed to be saved in
            $randomd=rand(0000000,9999999); //creates a random number as filename
            $domain = "http://".$_SERVER['HTTP_HOST'];  
            $file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work.
            $destination=$randomd.$file_ext; //new filename
            if ($file_ext=='pdf') {
                  move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination);
                  $pdf= $domain."/files/PDF/".$destination;                                             } else { echo("File type not supported.");  
  mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'");
                                                                              }
  </code></pre>
  
  <p>The if not empty does not work and it always tries to upload a file, but when I check the folder nothing is in there and it doesnt update the mysql.</p>