Arrays of several forms of loading, loading images, and then pasting into a database (PHP, MySQL)

Language: PHP / MySQL

I'll lose my mind, I really need to ask now ... I have a form for uploading multiple files:

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

With some Javascript, for every change made to this input, it adds a list of file names + a formatted string (grabbed from the file name) inside another input, so when replacing, we have a layout as shown below (assuming we just added some Images):

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


// HTML representation of such a layout will be ... (suppose 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 this because, in addition to downloading files, I would also like to add them to my database with a default header based on its file name (and with the ability to set / change this name for each image when it is downloaded). There is no problem with my form.

PROBLEM: My dilemma lies inside the PHP page on which the form data / action is presented.

I can only be:

  • Download the right images, but get the same name for everyone
  • Insert the correct headers, but get the same image for everyone

Here is my PHP action page: (currently loading the correct images, but has the same title for everyone)

 <?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 Colin Verot Download Class to handle image downloads and a guide to their FAQ for processing. MULTIPLE uploads images to this page in the section: How about multiple downloads? )

This would work perfectly if I just uploaded the images, however I added the functionality of adding each image to my database. and that's where he gets confused.

I am sure that the key places the SQL query inside the right foreach or maybe does another one, but I tried this and it gives me only 1 good result for loading an image or title, never others.

I need to upload an image to a site and then save its data (including the path to the images) in my database.

Please take a look at my code and enlighten me how to solve this problem? The key to the fragment would really be beautiful, as I am already very confused, having tried everything I could think of. Thank you very much!

+6
source share
3 answers

When you collect file information, you rewrite $imagename in each loop, so it will be assigned to the last. Try to bind it to the $files variable (hopefully this is not related to the upload class you are using).

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

Then update the $sql line to reference

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

You do not save the $ imagename variable to the $ files array, you just reload it every time.

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

It should be something like:

  $files[$i][$k] = array($v, $_POST['keyword'][$i]); ... foreach ($files as $data) { list($file, $imagename) = $data; ... } 
+2
source

I 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 go through each of the fields, assigning their value to the correct file, which corresponds to 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 rewrite var every time with the latter, looking, which means that you will only get one input file.

+2
source

Source: https://habr.com/ru/post/918973/


All Articles