How to store file path to MySQL database and files to a folder using PHP
Posted: Thu Mar 29, 2018 11:12 pm
- <?php
- if(isset($_POST['submit_button'])) {
- //theFile is name of file input in html
- if(is_uploaded_file($_FILES['theFile']['tmp_name'])) {
- $sourcePath = $_FILES['theFile']['tmp_name'];
- $fileName=$_FILES['theFile']['name'];
- if (!file_exists("myFolder")) {
- chmod(mkdir("myFolder",755));
- }
- $filePath = "myFolder/".$_FILES['theFile']['name'];
- $filePath = preg_replace('/\s+/', '', $filePath);
- //you can add other field also to save data to database,example
- $fname=$_POST['fname'];
- $lname=$_POST['lname'];
- //etc....
- if(move_uploaded_file($sourcePath,$filePath)) {
- $sql="INSERT INTO Table_name (firstname,firstname,filepath) VALUES('$fname',$lname','$filepath')";
- $results=mysqli_query($con, $sql) or die(mysqli_error($con));
- if ($results) {
- echo "success";
- }else{
- echo "failed";
- }
- }
- }
- }
- ?>