1 2 3 4 5 6 7 8 9 10 11 12 | CREATE TABLE `images` ( `id` int(11) NOT NULL, `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `uploaded_on` datetime NOT NULL, `status` enum( '1' , '0' ) COLLATE utf8_unicode_ci NOT NULL DEFAULT '1' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ALTER TABLE `images` ADD PRIMARY KEY (`id`); ALTER TABLE `images` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <!DOCTYPE html> <html> <head> <meta charset= "utf-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Upload Multiple Images and Store in Database using PHP and MySQL</title> </head> <body> <?php // Database configuration $dbHost = "localhost" ; $dbUsername = "root" ; $dbPassword = "" ; $dbName = "testingdb" ; // Create database connection $db = new mysqli( $dbHost , $dbUsername , $dbPassword , $dbName ); // Check connection if ( $db ->connect_error) { die ( "Connection failed: " . $db ->connect_error); } if (isset( $_POST [ 'submit' ])){ // File upload configuration $targetDir = "uploads/" ; $allowTypes = array ( 'jpg' , 'png' , 'jpeg' , 'gif' ); $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '' ; if (! empty ( array_filter ( $_FILES [ 'files' ][ 'name' ]))){ foreach ( $_FILES [ 'files' ][ 'name' ] as $key => $val ){ // File upload path $fileName = basename ( $_FILES [ 'files' ][ 'name' ][ $key ]); $targetFilePath = $targetDir . $fileName ; // Check whether file type is valid $fileType = pathinfo ( $targetFilePath ,PATHINFO_EXTENSION); if (in_array( $fileType , $allowTypes )){ // Upload file to server if (move_uploaded_file( $_FILES [ "files" ][ "tmp_name" ][ $key ], $targetFilePath )){ // Image db insert sql $insertValuesSQL .= "('" . $fileName . "', NOW())," ; } else { $errorUpload .= $_FILES [ 'files' ][ 'name' ][ $key ]. ', ' ; } } else { $errorUploadType .= $_FILES [ 'files' ][ 'name' ][ $key ]. ', ' ; } } if (! empty ( $insertValuesSQL )){ $insertValuesSQL = trim( $insertValuesSQL , ',' ); // Insert image file name into database $insert = $db ->query( "INSERT INTO images (file_name, uploaded_on) VALUES $insertValuesSQL" ); if ( $insert ){ $errorUpload = ! empty ( $errorUpload )? 'Upload Error: ' . $errorUpload : '' ; $errorUploadType = ! empty ( $errorUploadType )? 'File Type Error: ' . $errorUploadType : '' ; $errorMsg = ! empty ( $errorUpload )? '<br/>' . $errorUpload . '<br/>' . $errorUploadType : '<br/>' . $errorUploadType ; $statusMsg = "Files are uploaded successfully." . $errorMsg ; } else { $statusMsg = "Sorry, there was an error uploading your file." ; } } } else { $statusMsg = 'Please select a file to upload.' ; } // Display status message echo $statusMsg ; } ?> <p><h2>Upload Multiple Images and Store in Database using PHP and MySQLi</h2></p> <form action= "" method= "post" enctype= "multipart/form-data" > Select Image Files to Upload: <input type= "file" name= "files[]" multiple > <input type= "submit" name= "submit" value= "UPLOAD" > </form> </body> </html> |