RE: How to upload multiple files in php with add and remove options?
Hello,
Please tell me,
How to upload multiple files in php with add and remove options?
thanks
Hello, thanks for your questions,
I will show you how to upload multiple files in PHP with add and remove/delete options using PHP, javascript, and jquery. I have generated my personal PHP script which is very easy to understand and setup is also easy for any developers or users. You just need to follow the steps below.
I have fixed the add and delete/remove options also for multiple files upload in PHP. You can see the demo at the bottom of the page. Let’s start the tutorial.
Follow the steps for upload multiple files in php with add and remove options
Step 1: Create one “index.php” file and make one folder named “uploads” in your server
First of all, you need to create one file name “index.php” and create one folder name “uploads”.
Uploads folder is used for store all the uploaded files.
Step 2: Copy the below code and paste in “index.php”
The second step is you just need to copy the below code and paste it in your “index.php” file because This code is created by me and I have used the jquery for add new files and remove/delete selected files.
That’s it you can now execute the code and upload multiple files in PHP with add and remove/delete options using PHP, javascript, and jquery.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | <?php ini_set ( "error_reporting" , 1); if (isset( $_POST [ 'btnSubmit' ])) { for ( $i = 0; $i < count ( $_FILES [ 'files' ][ 'name' ]); $i ++) { if ( $_FILES [ "files" ][ "size" ][ $i ] < 1000000) // Check File size (Allow 1MB) { $temp = $_FILES [ "files" ][ "tmp_name" ][ $i ]; $name = $_FILES [ "files" ][ "name" ][ $i ]; if ( empty ( $temp )) { break ; } if ( $i == 0){ $err = "File uploaded successfully" ; $cls = "success" ; } move_uploaded_file( $temp , "img/" . $name ); } else { $err = "File size is more than 1MB" ; $cls = "danger" ; } } } ?> <html> <head> <style> body{ font-family:sans-serif; } table, th, td { border: 1px solid black; border-collapse: collapse; } .success{ color: green;} .danger{ color: red;} </style> </head> <body> <center> <form method= 'post' enctype= 'multipart/form-data' > <table id= "table" width= "50%" > <thead> <tr class = "text-center" > <th colspan= "3" style= "height:50px;" >Mutiple File Upload Script</th> </tr> </thead> <tbody> <tr class = "add_row" > <td id= "no" width= "5%" >#</td> <td width= "75%" ><input class = "file" name= 'files[]' type= 'file' multiple /></td> <td width= "20%" ></td> </tr> </tbody> <tfoot> <tr> <td colspan= "4" > <button class = "btn btn-success btn-sm" type= "button" id= "add" title= 'Add new file' />Add new file</button> </td> </tr> <tr class = "last_row" > <td colspan= "4" style= "text-align:center;" > <button class = "btn btn-primary submit" name= "btnSubmit" type= 'submit' >Upload</button> </td> </tr> </tfoot> </table> </form> <br><br><br><br> <h2 class = "<?php echo $cls; ?>" ><?php echo $err ; ?></h2> </center> <a href= "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" target= "_blank" rel= "nofollow" >https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js</a> $(document).ready( function (){ // Validation $( '.submit' ).click( function (){ var file_val = $( '.file' ).val(); if (file_val == "" ) { alert( "Please select at least one file." ); return false; } else { $( 'form' ).attr( 'action' , 'index.php' ); } }); // Append new row $( '#table' ).on( 'click' , "#add" , function (e) { $( 'tbody' ).append(' # <button type= "button" class = "btn btn-danger btn-sm" id= "delete" title= "Delete file" > Delete file</button> '); e.preventDefault(); }); // Delete row $( '#table' ).on( 'click' , "#delete" , function (e) { if (!confirm( "Are you sure you want to delete this file?" )) return false; $(this).closest( 'tr' ).remove(); e.preventDefault(); }); }); </body> </html> |
I hope you will like this tutorial and enjoyed it. If you have any questions then ask me again.