Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays from multiple upload form, Upload images then insert to database (PHP, MySQL)

Language: PHP / MySQL

I am going out of my mind, I really have to ask now... I have a multiple file upload form:

<input type="file" name="fileupload[]" multiple>

With the help of some Javascript, on each change made to this input, it appends a list of filenames, + a formatted string (grabbed from the filename) inside another input, so onchange we have a layout as shown below (assuming that we just added some images):

Multiple Upload FormAlmost similar to: http://jsfiddle.net/pxfunc/WWNnV/4/


// An HTML representation of such layout would be... (assuming that we added 3 images)

<input type="file" name="fileupload[]" multiple>

  • image-name-1.jpg   <input type="text" value="Image Name 1" name="keyword[]">
  • justsome_file.png   <input type="text" value="Justsome File" name="keyword[]">
  • some_Img-031.gif   <input type="text" value="Some Img 031" name="keyword[]">

<input type="submit" value="Upload">


I have it this way because aside from uploading the files, I would also like to add them to my database, with a default title based on its filename (and the option to set/change this title for each image as I upload it). There is no problem with my form.

PROBLEM: My dilemma lies inside the PHP page where the form data/action is submitted.

I can only manage to either:

  • Upload correct images, but get same title for all
  • Insert correct titles, but get same image for all

Here is my PHP action page: (Currently uploading correct images, but having same title for all)

<?php
// CONNECT TO DATABASE...
// INCLUDE UPLOAD CLASS LIBRARY
include (dirname(__FILE__).'/lib/class.upload.php');


$files = array();
foreach ($_FILES['fileupload'] as $k => $l)
{
    foreach ($l as $i => $v)
    {
        if (!array_key_exists($i, $files))
        $files[$i] = array();
        $files[$i][$k] = $v;
        $imagename = $_POST['keyword'][$i];
    }
}

// create an array here to hold file names
$uploaded = array();
foreach ($files as $file)
 {
            $generate_name = rand(100,99999); 
            $generate_name_extra = rand(200,9999);
            $filenamex = "COVER_PHOTO_".$generate_name.$generate_name_extra."_".time();
            $filenamex_thumb = $filenamex."_thumb";

            $handle = new upload($file);
            if ($handle->uploaded) {
            $this_upload = array();

            ///// 1 ////////////////////////////////////////////////////////////////////
            $handle->file_new_name_body   = $filenamex_thumb;
            $handle->file_force_extension = true;
            $handle->image_resize         = true;
            $handle->image_x              = '300';
            $handle->image_ratio_y        = true;
            $handle->jpeg_quality = '100';

            // ABSOLUTE PATH BELOW
            $handle->process($absoRoot.'covers/thumbs/');
            ////////////////////////////////////////////////////////////////////////////
            if ($handle->processed) {

      // store the image filename
    $this_upload['image'] = $handle->file_dst_name; // Destination file name
    $this_upload['body'] = $handle->file_dst_name_body; // Destination file name body
    $this_upload['extension'] = $handle->file_dst_name_ext; // Destination file extension

        $category_id = $_POST['cat'];
        $hiddenvalues = explode ("|",$_POST["cat"]);
        $category = $hiddenvalues[0];
        $category_name = $hiddenvalues[1];


                $sql = 'INSERT INTO cover (id, img, keyword, category_name, cat_id) VALUES ("", "'.$this_upload['image'].'", "'.$imagename.'", "'.$category_name.'", "'.$category.'")';
                 mysql_query($sql);
        }

    $handle->clean();
        header("Location: ./upload.php");
                $message = "";
    } else {

         echo '  file not uploaded to the wanted location';
         echo '  Error: ' . $handle->error . '';

      }
} ?>

(I use the Upload Class by Colin Verot to handle image uploads, and their FAQ tutorial to handle MULTIPLE image uploads on this page, under: What about multiple uploads?)

This would work perfect if I were just uploading images, however I added the functionality of adding each image data to my database. & This is where it gets confusing.

I'm sure the key is placing the SQL query inside the right foreach, or perhaps making another one, but I've tried that & it only gives me 1 good result for either the image upload or the title, never for both.

I need to upload the image to the site, then store its data (including image path) to my database.

Please look into my code and enlighten me how to solve this problem? A snippet clue would really be great for now as I am already very confused after having tried all I could think of. Thank you so much!

like image 429
Mafia Avatar asked Jun 25 '12 18:06

Mafia


2 Answers

You aren't saving your $imagename variable to the $files array, you're just resetting it each time.

    $files[$i][$k] = $v;
    $imagename = $_POST['keyword'][$i];

Should be something like:

    $files[$i][$k] = array($v, $_POST['keyword'][$i]);
    ...
    foreach ($files as $data) {
        list($file, $imagename) = $data;
        ...
    }
like image 102
nachito Avatar answered Oct 22 '22 08:10

nachito


I do think one of your problems is your foreach:

$files = array();
foreach ($_FILES['fileupload'] as $k => $l)
{
    foreach ($l as $i => $v)
    {
        if (!array_key_exists($i, $files))
        $files[$i] = array();
        $files[$i][$k] = $v;
        $imagename = $_POST['keyword'][$i];
    }
}

So you are going through each of the fields assigning their value to the right file which fits this structure policy:

_FILES => array(
    'name' => array(0 => 'file.txt'),
    'size' => array(0 => 235)
)

Which is correct for multifiles but then you do:

$imagename = $_POST['keyword'][$i];

Which does not look right. You are overwriting the var each time with the last looked at which means you will only ever get one input vlaue.

like image 37
Sammaye Avatar answered Oct 22 '22 06:10

Sammaye