├── .DS_Store ├── README.md ├── config └── database.php ├── file-upload.php └── index.php /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SinghDigamber/php-multiple-file-upload/54edebd51cfb4eb487ff73f172a1ceca966f9a0f/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-multiple-file-upload 2 | PHP 7 Multiple Files and Images uploading tutorial, and we will learn to store uploaded files in the MySQL database along with some necessary file uploading validation. 3 | 4 | [PHP 7 Multiple Files/Images Upload in MySQL Database](https://www.positronx.io/php-multiple-files-images-upload-in-mysql-database/) -------------------------------------------------------------------------------- /config/database.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 11 | //echo "Database connected successfully"; 12 | } catch(PDOException $e) { 13 | echo "Database connection failed: " . $e->getMessage(); 14 | } 15 | 16 | ?> -------------------------------------------------------------------------------- /file-upload.php: -------------------------------------------------------------------------------- 1 | $val){ 15 | // Get files upload path 16 | $fileName = $_FILES['fileUpload']['name'][$id]; 17 | $tempLocation = $_FILES['fileUpload']['tmp_name'][$id]; 18 | $targetFilePath = $uploadsDir . $fileName; 19 | $fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION)); 20 | $uploadDate = date('Y-m-d H:i:s'); 21 | $uploadOk = 1; 22 | 23 | if(in_array($fileType, $allowedFileType)){ 24 | if(move_uploaded_file($tempLocation, $targetFilePath)){ 25 | $sqlVal = "('".$fileName."', '".$uploadDate."')"; 26 | } else { 27 | $response = array( 28 | "status" => "alert-danger", 29 | "message" => "File coud not be uploaded." 30 | ); 31 | } 32 | 33 | } else { 34 | $response = array( 35 | "status" => "alert-danger", 36 | "message" => "Only .jpg, .jpeg and .png file formats allowed." 37 | ); 38 | } 39 | // Add into MySQL database 40 | if(!empty($sqlVal)) { 41 | $insert = $conn->query("INSERT INTO user (images, date_time) VALUES $sqlVal"); 42 | if($insert) { 43 | $response = array( 44 | "status" => "alert-success", 45 | "message" => "Files successfully uploaded." 46 | ); 47 | } else { 48 | $response = array( 49 | "status" => "alert-danger", 50 | "message" => "Files coudn't be uploaded due to database error." 51 | ); 52 | } 53 | } 54 | } 55 | 56 | } else { 57 | // Error 58 | $response = array( 59 | "status" => "alert-danger", 60 | "message" => "Please select a file to upload." 61 | ); 62 | } 63 | } 64 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 |