totallytech Posted April 23, 2018 Share Posted April 23, 2018 Hi All, I'm trying to create a plugin for my wordpress site, and I'm trying to upload multiple images via a form, then I want to run a script that inserts the filename in a database... I can get it to work, but only for one image at a time - even if I select 5 images, it just uploads one image and inserts the filename to the database... My current form is: <form method="post" enctype="multipart/form-data"> <input type="file" name="my_file[]" multiple="multiple"><br><br> <input class="button" type="submit" value="Upload"> </form> This is my current code: <?php $school_id = $_GET["id"]; $upload_dir = wp_upload_dir(); $file_dir = $upload_dir['basedir'] . '/schoolio/' . $school_id .'/'; //check if the directory exists if (!file_exists($photo_dir)) { wp_mkdir_p( $file_dir ); } //check if the file is selected and count the number of files if (isset($_FILES['my_file'])) { $myFile = $_FILES['my_file']; $fileCount = count($myFile["name"]); // this loop will run equal to the file count and save the uploaded file in folder for ($i = 0; $i < $fileCount; $i++) { if (move_uploaded_file($myFile["tmp_name"][$i] , $file_dir.$myFile["name"][$i])) { echo "The file ". $myFile["name"][$i]. " has been uploaded.<BR/>"; //insert global $wpdb; $table_name = $wpdb->prefix . "files"; $wpdb->insert( $table_name, array( 'school_id' => $school_id, 'file_id' => $myFile["name"][$i], ), array( '%s', '%s', ) ); } } } ?> Can anyone see why its only doing one file instead of the loop? (If I remove the insert code then it does all the files in the loop fine) Thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.