简体   繁体   中英

Saving a PDF file using PHP

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:

    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'");
                                                                            }

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.

$_FILES['pdf'] will never be empty(when the form has been submitted), no matter if a file has been selected or not, it will always return an array.

Check $_FILES['pdf']['error'] , it will be 4 when no file has been uploaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM