How to store file path to MySQL database and files to a folder using PHP

Post Reply
cache
Member
Reactions: 12
Posts: 22
Joined: 6 years ago

#1

  1. <?php
  2. if(isset($_POST['submit_button'])) {
  3.   //theFile is name of file input in html
  4. if(is_uploaded_file($_FILES['theFile']['tmp_name'])) {
  5.  
  6. $sourcePath = $_FILES['theFile']['tmp_name'];
  7. $fileName=$_FILES['theFile']['name'];
  8. if (!file_exists("myFolder")) {
  9.       chmod(mkdir("myFolder",755));
  10.      }
  11. $filePath = "myFolder/".$_FILES['theFile']['name'];
  12. $filePath = preg_replace('/\s+/', '', $filePath);
  13.  
  14. //you can add other field also to save data to database,example
  15. $fname=$_POST['fname'];
  16. $lname=$_POST['lname'];
  17. //etc....
  18.  
  19. if(move_uploaded_file($sourcePath,$filePath)) {
  20.   $sql="INSERT INTO Table_name (firstname,firstname,filepath) VALUES('$fname',$lname','$filepath')";
  21. $results=mysqli_query($con, $sql) or die(mysqli_error($con));
  22.  
  23.   if ($results) {
  24.     echo "success";
  25.   }else{
  26.     echo "failed";
  27.   }
  28.        }
  29. }
  30. }
  31. ?>

1
1 Image
Post Reply

Return to “Software Engineering”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests