karthika pommudu

karthika pommudu

  • NA
  • 321
  • 32.3k

cannot insert .pdf file to mysql db

Aug 5 2020 4:12 AM
Hi,
 
I tried to insert pdf file to database actually the file uploaded into the folder name 'upload ' but cannot insert into database.
 
Anyone can help me to clear this issue. 
  1. <div class="row">  
  2.     <div class="col-xs-8 col-xs-offset-2 well">  
  3.     <form  method="post" enctype="multipart/form-data">  
  4.         <legend>Select File to Upload:</legend>  
  5.         <div class="form-group">  
  6.             <input type="file" name="file1" />  
  7.         </div>  
  8.         <div class="form-group">  
  9.             <input type="submit" name="submit" value="Upload" class="btn btn-info"/>  
  10.         </div>  
  11.           
  12.     </form>  
  13.     </div>  
  14. </div>  
  1. if (isset($_POST['submit']))  
  2. {  
  3.      
  4. echo $filename = $_FILES['file1']['name'];  
  5.   
  6.     // destination of the file on the server  
  7.     $destination = 'upload/' . $filename;  
  8.   
  9.     // get the file extension  
  10.     $extension = pathinfo($filename, PATHINFO_EXTENSION);  
  11.   
  12.     // the physical file on a temporary uploads directory on the server  
  13.     $file = $_FILES['file1']['tmp_name'];  
  14.    echo  $size = $_FILES['file1']['size'];  
  15.   
  16.     if (!in_array($extension, ['pdf'])) {  
  17.         echo "You file extension must be .pdf ";  
  18.     } elseif ($_FILES['myfile']['size'] > 1000000) { // file shouldn't be larger than 1Megabyte  
  19.         echo "File too large!";  
  20.     } else {  
  21.         echo 'test';   
  22.         // move the uploaded (temporary) file to the specified destination  
  23.         if (move_uploaded_file($file, $destination))   
  24.         {  
  25.               
  26.             $sql = "INSERT INTO files (id,filename,created) VALUES ('1','$filename', '1')";  
  27.             if (mysqli_query($link, $sql))   
  28.             {  
  29.                 echo "File uploaded successfully";  
  30.             }  
  31.             else   
  32.             {  
  33.                 echo 'Failed to insiert the file.';  
  34.             }  
  35.         } else   
  36.         {  
  37.             echo "Failed to upload file.";  
  38.         }  
  39.     }  
  40. }  
 

Answers (4)