Php скачивание нескольких файлов. Загрузка файлов на сервер с помощью PHP
Multiple files can be uploaded using different name for input .
It is also possible to upload multiple files simultaneously and have the information organized automatically in arrays for you. To do so, you need to use the same array submission syntax in the HTML form as you do with multiple selects and checkboxes:
Warning
As of PHP 5.2.12, the max_file_uploads configuration setting acts as a limit on the number of files that can be uploaded in one request. You will need to ensure that your form does not try to upload more files in one request than this limit.
14 years ago
When uploading multiple files, the $_FILES variable is created in the form:
Array
=> Array
=> foo.txt
=> bar.txt
)
=> Array
=> text/plain
=> text/plain
)
=> Array
=> /tmp/phpYzdqkD
=> /tmp/phpeEwEWG
)
=> Array
=> 0
=> 0
)
=> Array
=> 123
=> 456
)
I found it made for a little cleaner code if I had the uploaded files array in the form
Array
=> Array
=> foo.txt
=> text/plain
=> /tmp/phpYzdqkD
=> 0
=> 123
)
=> Array
=> bar.txt
=> text/plain
=> /tmp/phpeEwEWG
=> 0
=> 456
)
I wrote a quick function that would convert the $_FILES array to the cleaner (IMHO) array.
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:
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"
];
}
}
?>
5 years ago
This is also needed for elements.
So, if you have an input element like this:
This should be written as
else you"ll only be able to get one of the files.
9 years ago
Here is a function to fix the indices of a multi-dimensional for easier parsing when dealing with file uploads. It takes a single $_FILES field array as a parameter and separates each individual uploaded file by numeric key. This allows for iterating like:
fixFilesArray
($_FILES
[
"array_of_files"
]);
foreach ($_FILES
[
"array_of_files"
] as
$position
=>
$file
) {
// should output array with indices name, type, tmp_name, error, size
var_dump
($file
);
}
?>
Here"s the code:
/**
* Fixes the odd indexing of multiple file uploads from the format:
*
* $_FILES["field"]["key"]["index"]
*
* To the more standard and appropriate:
*
* $_FILES["field"]["index"]["key"]
*
* @param array $files
* @author Corey Ballou
* @link http://www.jqueryin.com
*/
function
fixFilesArray
(&
$files
)
{
$names
= array("name"
=>
1
,
"type"
=>
1
,
"tmp_name"
=>
1
,
"error"
=>
1
,
"size"
=>
1
);
Foreach ($files
as
$key
=>
$part
) {
// only deal with valid keys and multiple files
$key
= (string)
$key
;
if (isset($names
[
$key
]) &&
is_array
($part
)) {
foreach ($part
as
$position
=>
$value
) {
$files
[
$position
][
$key
] =
$value
;
}
// remove old key reference
unset($files
[
$key
]);
}
}
}
?>
3 years ago
This is a very simple example:
echo "
" ;
$img = $_FILES [ "img" ];if(!empty($img ))
{
$img_desc = reArrayFiles ($img );
print_r ($img_desc );Foreach($img_desc as $val )
{
$newname = date ("YmdHis" , time ()). mt_rand (). ".jpg" ;
move_uploaded_file ($val [ "tmp_name" ], "./uploads/" . $newname );
}
}function reArrayFiles ($file )
{
$file_ary = array();
$file_count = count ($file [ "name" ]);
$file_key = array_keys ($file );For($i = 0 ; $i < $file_count ; $i ++)
{
foreach($file_key as $val )
{
$file_ary [ $i ][ $val ] = $file [ $val ][ $i ];
}
}
return $file_ary ;
}2 years ago
Once I had to do a maintenance in a huge ERP that had several multiple upload inputs inside an array. Just like this:
The $_FILES array is created like this:
Array
=> Array
=> Array
=> teste.c
=> teste
)
=> Array
=> text/x-csrc
=> application/octet-stream
)
=> Array
=> /opt/lampp/temp/phpuf3KNj
=> /opt/lampp/temp/php0yPZap
)
=> Array
=> 0
=> 0
)
=> Array
=> 1960
=> 8661
)
=> Array
=> Array
=> jefrey.html
=> notas.txt
)
=> Array
=> text/html
=> text/plain
)
=> Array
=> /opt/lampp/temp/php87nfyu
=> /opt/lampp/temp/phpUBlvVz
)
=> Array
=> 0
=> 0
)
=> Array
=> 583
=> 191
)
I"ve managed to re-arrange this array like this:
Array
=> Array
=> Array
=> teste.c
=> text/x-csrc
=> /opt/lampp/temp/phpuf3KNj
=> 0
=> 1960
)
=> Array
=> teste
=> application/octet-stream
=> /opt/lampp/temp/php0yPZap
=> 0
=> 8661
)
=> Array
=> Array
=> jefrey.html
=> text/html
=> /opt/lampp/temp/php87nfyu
=> 0
=> 583
)
=> Array
=> notas.txt
=> text/plain
=> /opt/lampp/temp/phpUBlvVz
=> 0
=> 191
)
Here"s my snippet:
function
reArrayFilesMultiple
(&
$files
) {
$uploads
= array();
foreach($_FILES
as
$key0
=>
$FILES
) {
foreach($FILES
as
$key
=>
$value
) {
foreach($value
as
$key2
=>
$value2
) {
$uploads
[
$key0
][
$key2
][
$key
] =
$value2
;
}
}
}
$files
=
$uploads
;
return
$uploads
;
// prevent misuse issue
}
?>
3 years ago
This is just a modification of the code which is the top note by "phpuser" here. His/her version requires that the $file_post array passed in to the function was created by a form submitted with the multiple attribute set. With multiple set in the html input tag, $_FILES["fileInputName"]["name"] is an array no matter if only one file is sent or multiple. But when is used without the multiple attribute then $_FILES["fileInputName"]["name"] is not an array, it contains the the string with the filename. To use this neat function with or without multiple set and to get back an array which you can "foreach" over in either case, use this modification:
function reArrayFiles(&$file_post)
{
$file_ary = array();
$multiple = is_array($file_post["name"]);
$file_count = $multiple ? count($file_post["name"]) : 1;
$file_keys = array_keys($file_post);
For ($i=0; $i<$file_count; $i++)
{
foreach ($file_keys as $key)
{
$file_ary[$i][$key] = $multiple ? $file_post[$key][$i] : $file_post[$key];
}
}
Return $file_ary;
}
3 years ago
Recursive solution for complex situations (supports any nested arrays including indexed arrays)
function getFixedFilesArray() {
$walker = function ($arr, $fileInfokey, callable $walker) {
$ret = array();
foreach ($arr as $k => $v) {
if (is_array($v)) {
$ret[$k] = $walker($v, $fileInfokey, $walker);
} else {
$ret[$k][$fileInfokey] = $v;
}
}
return $ret;
};
$files = array();
foreach ($_FILES as $name => $values) {
// init for array_merge
if (!isset($files[$name])) {
$files[$name] = array();
}
if (!is_array($values["error"])) {
// normal syntax
$files[$name] = $values;
} else {
// html array feature
foreach ($values as $fileInfoKey => $subArray) {
$files[$name] = array_replace_recursive($files[$name], $walker($subArray, $fileInfoKey, $walker));
}
}
}
Return $files;
}
10 years ago
If you try and upload files with multi-dimensional names like this:
You will get an unexpected format like this:
array(
"submission"
=> array
"name"
=> array("screenshot"
=>
"monster_wallpaper.jpg"
),
"type"
=> array("screenshot"
=>
"image/jpeg"
),
"tmp_name"
=> array("screenshot"
=>
"/tmp/php48lX2Y"
),
"error"
=> array("screenshot"
=>
0
),
"size"
=> array("screenshot"
=>
223262
),
),
....
?>
You can use the following function to re-format the array recursively in the usual format:
function
format_files_array
($files
,
$name
=
null
, &
$new
=
false
,
$path
=
false
){
$names
= array("name"
=>
"name"
,
"type"
=>
"type"
,
"tmp_name"
=>
"tmp_name"
,
"error"
=>
"error"
,
"size"
=>
"size"
);
Foreach($files
as
$key
=> &
$part
)
{
$key
= (string)
$key
;
if(in_array
($key
,
$names
))
$name
=
$key
;
if(!
in_array
($key
,
$names
))
$path
=
$key
;
if(is_array
($part
))
$part
=
format_files_array
($part
,
$name
,
$new
,
$path
);
elseif(!
is_array
($part
))
{
$current
=&
$new
;
foreach($path
as
$p
)
$current
=&
$current
[
$p
];
$current
[
$name
] =
$part
;
unset($path
);
$name
=
null
;
}
}
Return
$new
;
}
?>
Приложение для загрузки файлов на сервер представляет собой HTML-форму (upload.html) и скрипт upload.php для ее обработки.
Замечание: Вы можете загрузить промышленную версию системы загрузки файлов на сервер из раздела . Система позволит вам не только загрузить файл на сервер, но и изменить его размер, фон и др.
Код формы (upload.html)
Форма для загрузки файлов