Asked  8 Months ago    Answers:  5   Viewed   30 times

I need to upload multiple images via form. I thought that I will do it with no problem, but I have one.

When I try to do foreach and get image by image it is not acting like I hoped it will.

HTML

<form method="post" action="" enctype="multipart/form-data" id="frmImgUpload">
    <input name="fileImage[]" type="file" multiple="true" />
    <br />
    <input name="btnSubmit" type="submit" value="Upload" />
</form>

PHP

<?php
if ($_POST)
{
    echo "<pre>";
    foreach ($_FILES['fileImage'] as $file)
    {
        print_r($file);
        die(); // I want it to print first image content and then die to test this out...
        //imgUpload($file) - I already have working function that uploads one image
    }
}

What I expected from it to print out first image, instead it prints names of all the images.

Example

Array
(
    [0] => 002.jpg
    [1] => 003.jpg
    [2] => 004.jpg
    [3] => 005.jpg
)

What I want it to output

Array
(
    [name] => 002.jpg
    [type] => image/jpeg
    [tmp_name] => php68A5.tmp
    [error] => 0
    [size] => 359227
)

So how can I select image by image in the loop so I can upload them all?

Okey I found solution and this is how I did it, probably not the best way but it works.

foreach ($_FILES['fileImage']['name'] as $f)
{
    $file['name'] = $_FILES['fileImage']['name'][$i];
    $file['type'] = $_FILES['fileImage']['type'][$i];
    $file['tmp_name'] = $_FILES['fileImage']['tmp_name'][$i];
    $file['error'] = $_FILES['fileImage']['error'][$i];
    $file['size'] = $_FILES['fileImage']['size'][$i];
    imgUpload($file);
    $i++;
}

 Answers

18

You are basically asking of how to rebuild the $_FILES array to access subitems of them as one array.

$index = 0;
$field = 'fileImage';
$keys = array_keys($_FILES[$field]);
$file = array();
foreach($keys as $key)
{
    $file[$key] = $_FILES[$field][$key][$index];
}
print_r($file);

change $index to the value you need to pick a specific file.

Wednesday, March 31, 2021
 
rblarsen
answered 8 Months ago
23

Upload all images at once :

<form method="post" action="" enctype="multipart/form-data" id="frmImgUpload">
    <input name="multiple_uploaded_files[]" type="file" multiple="true" />
</form>

foreach ($_FILES['multiple_uploaded_files']['name'] as $file)
    {
        print_r($file);
        //use the move_uploaded_file() to move your file on your server directory.

        //fire an insert query that inserts all the file names with comma separated value
    }

IF you want the user to upload images separately,one by one :

<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >

foreach ($_FILES['uploaded_file']['name'] as $file)
        {
            print_r($file);
            //use the move_uploaded_file() to move your file on your server directory.
            //fire an insert query that inserts all the file names with comma separated value
        }
Friday, May 28, 2021
 
Lorav
answered 5 Months ago
58

Change the markup as follows:

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="file[]" type="file" multiple="multiple" /><br />
  <input type="submit" value="Send files" />
</form>

Now, you can loop through the $_FILES array using a foreach, like so:

foreach ($_FILES['file']['tmp_name'] as $index => $tmpName) {
    if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
    {
        // $tmpName is the file
        // code for sending the image to imgur
    }
}
Saturday, May 29, 2021
 
macha
answered 5 Months ago
45

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

Thursday, August 5, 2021
 
Nil
answered 3 Months ago
Nil
80

A recent change in PHPMailer allows Debugoutput to be a closure, so you can make it do whatever you like, for example to collect all the debug output and emit it later:

$debug = '';
$mail->Debugoutput = function($str, $level) {
    $GLOBALS['debug'] .= "$level: $strn";
};
//...later
echo $debug;
Monday, August 16, 2021
 
samayo
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :