Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php upload multiple files

When uploading multiple files, the $_FILES variable is created in the form:

Array
(
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

I found it made for a little cleaner code if I had the uploaded files array in the form

Array
(
    [0] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpYzdqkD
            [error] => 0
            [size] => 123
        )

    [1] => Array
        (
            [name] => bar.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpeEwEWG
            [error] => 0
            [size] => 456
        )
)

I wrote a quick function that would convert the $_FILES array to the cleaner (IMHO) array.

<?php

function reArrayFiles(&$file_post) {

    $file_ary = array();
    $file_count = count($file_post['name']);
    $file_keys = array_keys($file_post);

    for ($i=0; $i<$file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_ary;
}

?>

Now I can do the following:

<?php

if ($_FILES['upload']) {
    $file_ary = reArrayFiles($_FILES['ufile']);

    foreach ($file_ary as $file) {
        print 'File Name: ' . $file['name'];
        print 'File Type: ' . $file['type'];
        print 'File Size: ' . $file['size'];
    }
}

?>
Comment

php include multiple files at once

array_map( function ($a) { return include($a); }, array('xx.php','yy.php','zz.php'));
Comment

PREVIOUS NEXT
Code Example
Php :: laravel model 
Php :: laravel fontawesome blade directive 
Php :: php get current page url 
Php :: laravel create session table 
Php :: EntityManager get repository 
Php :: php dirpath multiple file extensions 
Php :: refresh laravel model 
Php :: how to setup php mailer 
Php :: php http method 
Php :: laravel passport client 
Php :: php pdo setting error modes 
Php :: Laravel jwt check token sent by request is valid 
Php :: Cambiar la imagen por defecto en producto WooCommerce 
Php :: php combine 2 arrays keep duplicates 
Php :: laravel queue timeout 
Php :: laravel include config 
Php :: php convert latitude longitude to map tile 
Php :: php get filename 
Php :: PHP trim — Strip whitespace (or other characters) from the beginning and end of a string 
Php :: FPDF invoice Tutorial 
Php :: laravel 8 seeding 
Php :: nested with laravel 
Php :: how to save and get checkbox value in database php 
Php :: how to run a php file in xampp 
Php :: wordpress add shortcode with parameters 
Php :: how to declare global variable in laravel controller 
Php :: Laravel query where and 
Php :: woocommerce disable links on specific product 
Php :: firstOrFail() 
Php :: laravel pagination limit page 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =