Jump to content

Help uploading images and listing the filename in DB


totallytech

Recommended Posts

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.