Asked  7 Months ago    Answers:  5   Viewed   30 times

Here are the inputs I want to loop through

Main photo:   <input type="file" name="image[]" />
Side photo 1: <input type="file" name="image[]" />
Side photo 2: <input type="file" name="image[]" />
Side photo 3: <input type="file" name="image[]" />

A couple weird things happened, when I uploaded nothing I use the count($_FILES['image']), I echoed that function, and it returns a value of 5. There should be no elements in that array. Why is there one extra input when I only have 4 files to begin with?

Now with the actually looping itself, I try using the foreach loop, but it doesn't work.

foreach($_FILES['image'] as $files){echo $files['name']; }

Nothing came up, what I wanted to ultimately do is to loop through all images, make sure they are correct format, size, and rename each of them. But this simple foreach() loop shows that somehow I can't even loop through the $_FILES array and the count() confused me even more when it say that there are 5 elements in the array when I didn't even upload anything.

 Answers

71

Your example form should work fine. It's just that you are expecting the structure of the $_FILES superglobal to be different than it actually is, when using an array structure for the field names.

The structure of this multidimensional array is as followed then:

$_FILES[fieldname] => array(
    [name] => array( /* these arrays are the size you expect */ )
    [type] => array( /* these arrays are the size you expect */ )
    [tmp_name] => array( /* these arrays are the size you expect */ )
    [error] => array( /* these arrays are the size you expect */ )
    [size] => array( /* these arrays are the size you expect */ )
);

Therefor count( $_FILES[ "fieldname" ] ) will yield 5.
But counting deeper dimensions will also not produce the result you may expect. Counting the fields with count( $_FILES[ "fieldname" ][ "tmp_name" ] ) for instance, will always result in the number of file fields, not in the number of files that have actually been uploaded. You'd still have to loop through the elements to determine whether anything has been uploaded for a particular file field.

EDIT
So, to loop through the fields you would do something like the following:

// !empty( $_FILES ) is an extra safety precaution
// in case the form's enctype="multipart/form-data" attribute is missing
// or in case your form doesn't have any file field elements
if( strtolower( $_SERVER[ 'REQUEST_METHOD' ] ) == 'post' && !empty( $_FILES ) )
{
    foreach( $_FILES[ 'image' ][ 'tmp_name' ] as $index => $tmpName )
    {
        if( !empty( $_FILES[ 'image' ][ 'error' ][ $index ] ) )
        {
            // some error occured with the file in index $index
            // yield an error here
            return false; // return false also immediately perhaps??
        }

        /*
            edit: the following is not necessary actually as it is now 
            defined in the foreach statement ($index => $tmpName)

            // extract the temporary location
            $tmpName = $_FILES[ 'image' ][ 'tmp_name' ][ $index ];
        */

        // check whether it's not empty, and whether it indeed is an uploaded file
        if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
        {
            // the path to the actual uploaded file is in $_FILES[ 'image' ][ 'tmp_name' ][ $index ]
            // do something with it:
            move_uploaded_file( $tmpName, $someDestinationPath ); // move to new location perhaps?
        }
    }
}

For more information see the docs.

Wednesday, March 31, 2021
 
ranhan
answered 7 Months ago
34

First you need to create temporary .fdf file with input data and then use pdftk to mearge template pdf and .fdf to create filled pdf.

function createFDF($file,$info) {
    $data = "%FDF-1.2n%âãÏÓn1 0 objn<< n/FDF << /Fields [ ";

    foreach ($info as $field => $val) {
        if (is_array($val)) {
            $data .= '<</T('.$field.')/V[';

            foreach ($val as $opt) {
                $data .= '('.trim($opt).')';
            }

            $data .= ']>>';
        } else {
            $data .= '<</T('.$field.')/V('.trim($val).')>>';
        }
    }

    $data .= "] n/F (".$file.") /ID [ <".md5(time()).">n] >>" .
        " n>> nendobjntrailern" .
        "<<n/Root 1 0 R nn>>n%%EOFn";

    return $data;
}


$fileName = "Template_PDF.pdf";
$fdf_file = "temporary.fdf";
$result_file = "Result.pdf";
$arr['a_variable'] = 'value or a variable';

$fdf_data = createFDF($fileName, $arr);

if ($fp=fopen($fdf_file,'w')) {
    fwrite($fp,$fdf_data,strlen($fdf_data));
} else {
    echo 'Unable to create file: ' . $fdf_file . '<br>';
}

fclose($fp);
exec("pdftk " . $fileName . " fill_form " . $fdf_file . " output " . $result_file);

thats it.

Wednesday, March 31, 2021
 
jcubic
answered 7 Months ago
32

Simply use two nested for loops. To get the sizes of the dimensions, you can use GetLength():

for (int i = 0; i < arrayOfMessages.GetLength(0); i++)
{
    for (int j = 0; j < arrayOfMessages.GetLength(1); j++)
    {
        string s = arrayOfMessages[i, j];
        Console.WriteLine(s);
    }
}

This assumes you actually have string[,]. In .Net it's also possible to have multidimensional arrays that aren't indexed from 0. In that case, they have to be represented as Array in C# and you would need to use GetLowerBound() and GetUpperBound() the get the bounds for each dimension.

Tuesday, August 3, 2021
 
Jeremy Pare
answered 3 Months ago
11

mysql_fetch_row fetches each row one at a time. In order to retrieve multiple rows, you would use a while loop like this:

while ($row = mysql_fetch_row($result))
{
    // code
}
Saturday, August 7, 2021
 
ericstumper
answered 3 Months ago
94

If you don't know the key and want the current element of the array, you could use:

$challengename = current($this->challengenames);

Or the first element of the array:

$challengename = reset($this->challengenames);

Or the last element of the array:

$challengename = end($this->challengenames);

Just note that end and reset will change the pointer location of the array.

Thursday, August 26, 2021
 
aWebDeveloper
answered 2 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 :