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 request input default value 
Php :: wordpress query get results 
Php :: header() php 
Php :: $ is not define 
Php :: laravel bootstrap navbar active 
Php :: append in php 
Php :: set cookie on button click JavaScript 
Php :: laravel pagination with search filter 
Php :: PHP: How to remove specific element from an array? 
Php :: laravel custom pagination 
Php :: $_server php 
Php :: how to determine if file is empty in php 
Php :: laravel adding condition to relation 
Php :: php get first two paragraphs 
Php :: removing the last value of an array 
Php :: php concatenation with a space 
Php :: error laravel 404 in server 
Php :: php tutorial 
Php :: how do i use $variables as values in php 7 mysqli insert 
Php :: php artisan serve stop 
Php :: php creating a subdomain automatically in cpanel 
Php :: get romawi number php 
Php :: cmd run php file 
Php :: php glob sort by unsigned int 
Php :: php huruf besar di awal 
Php :: validar tipo de imagen php 
Php :: How to create routes in the codeigniter 
Php :: laravel change db connection on the fly 
Php :: static function php 
Php :: laravel copy image 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =