How to Loop Through $_Files Array

How do you loop through $_FILES array?

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.

How to loop through $_FILES?

Loop ever the $_FILES array and use the ['name'] inside the iteration:

$count = count($_FILES['files'])
for($i=0; $i<=$count; $i++) {
if ($_FILES['files'][$i]['size'])
echo $_FILES['files'][$i]['name']."\n";
}

Even easier is to use a foreach loop:

foreach($_FILES['files'] as $file) {
if($file['size'])
echo $file['name']."\n";
}

How to Loop through multi dimensional array of $_FILES array

You can use this:

$keys = array_keys($_FILES); // get all the fields name
$res = array_map(null, ...array_values($_FILES)); // group the array by each file
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res); // insert the field name to result array

Documentation:

array-keys, array-map and array-combine

Live example: 3v4l

how to loop through FormData using php

Foreach looks like this in php

PHP: foreach

And this is how $_FILES looks

PHP: $_FILES

So in Upload.php you may change your code from

foreach(name in $_FILES['']) // how can I loop here on DataForm and get each tmpname??
{
console.log( $_FILES['name']['tmp_name']);
}

to

foreach($_FILES as $key => $file ) { // $key is index and $file is item
echo "this is ".$file['tmp_name']; // like console.log in js
}

Now you can interate on $_FILES variable, It means will access on each temp_name

How to loop through each line in a file PHP

Your code will make some empty lines when erases key from file.
And you passed return value of fopen() as a parameter of file_get_contents(). the return value is resource on file, but file_get_contents() needs string(file path).

You can check following.

$gkey = $_GET["key"];

$file_path="./generatedkeys.txt";
$file = fopen($file_path, "a");
$generatedk = array();

// generate table from data in txt file
while(! feof($file)) {
$generatedk[] = fgets($file);
}
fclose($file);

for($i=0; $i<count($generatedk); $i++){
$key=$generatedk[$i];
if ($key == hash("sha256", $gkey)){

// Removing of key from data in txt file
array_splice($generatedk, $i, 1);
file_put_contents($file_path, implode("\n", $generatedk));

$accfile = fopen("./accs.txt", "a");
fwrite($accfile, hash("sha256", $key).",".hash("sha256", $hwid)."\n");
fclose($accfile);

break;
}
}

I hope it will be working well.
Thanks.

How can I iterate PHP $_FILES array?

Wow, that indeed is odd, are you using <input type="file" name="attachments[]" /> in your markup? If you could afford to change those using unique name=, you won't have that odd format...

To directly answer your question, try:

$len = count($_FILES['attachments']['name']);

for($i = 0; $i < $len; $i++) {
$fileSize = $_FILES['attachments']['size'][$i];
// change size to whatever key you need - error, tmp_name etc
}

PHP - Looping through $_FILES to check filetype

The && operator has a higher precedence than ||, so rather than (A OR B OR C) AND D as you intended, it is actually A OR B OR (C AND D)

You can use parentheses to enforce the evaluation you intended.

However, something like this might be cleaner and easier to maintain/read:

$allowed_types=array(
'image/gif',
'image/jpeg',
'image/png',
);

$sscount = $_POST['imgcount'];
if($sscount>0){
for($i = 1; $i <= $sscount; $i++){

if (in_array($_FILES["image$i"]["type"], $allowed_types) &&
($_FILES["image$i"]["size"] < 500000))
{

}

}
}

How to loop through HTML5 multiple file upload?

$_FILES is multidimensional. With the syntax that you used, the Filenames would be accessible in

$_FILES['image_name']['name'][0] # for the first one
$_FILES['image_name']['name'][1] # for the second one

and so on.
Here is a link for reference Uploading Multiple Files



Related Topics



Leave a reply



Submit