[英]PHP Handle Multiple File Uploads EASY
解决了! 使用来自php.net post方法的此示例来处理多个上载,并获得了使用andre的答案所获得的知识,这就是为什么我选择它作为答案的原因!
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
if ($x=="0"){
$data=explode(".",$_FILES["userfile"]["name"][$key]);
$named=$id[id].".".$data[1];
$x=$x+1;
}else{
$data=explode(".",$_FILES["userfile"]["name"][$key]);
$named=$id[id]."-".$x.".".$data[1];
$x=$x+1;
}
$uploaddir = '/home/content/92/8498392/html/items/'.$_POST[catid];
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
move_uploaded_file($tmp_name, "$uploaddir/$named");
}
}
结果是第一个图像以45.jpg上传,第二个图像以45-1.jpg上传,依此类推! 谢谢你们的帮助:D
问题是您如何遍历$ _FILES数组。 它不能像您想象的那样处理多个文件,因此请使用以下功能并将其合并到脚本中。 以下脚本的功劳归于PHP网站上的家伙。 链接在代码下方。
function multiple(array $_files, $top = TRUE)
{
$files = array();
foreach($_files as $name=>$file){
if($top) $sub_name = $file['name'];
else $sub_name = $name;
if(is_array($sub_name)){
foreach(array_keys($sub_name) as $key){
$files[$name][$key] = array(
'name' => $file['name'][$key],
'type' => $file['type'][$key],
'tmp_name' => $file['tmp_name'][$key],
'error' => $file['error'][$key],
'size' => $file['size'][$key],
);
$files[$name] = multiple($files[$name], FALSE);
}
}else{
$files[$name] = $file;
}
}
return $files;
}
print_r($_FILES);
/*
Array
(
[image] => Array
(
[name] => Array
(
[0] => 400.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => /tmp/php5Wx0aJ
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 15726
)
)
)
*/
$files = multiple($_FILES);
print_r($files);
/*
Array
(
[image] => Array
(
[0] => Array
(
[name] => 400.png
[type] => image/png
[tmp_name] => /tmp/php5Wx0aJ
[error] => 0
[size] => 15726
)
)
)
*/
?>
我是从PHP网站上获取的
<?php
$id = 877;
$x = 0;
foreach ($_FILES as $file) {
$data = explode(".", $file['userfile']['name']);
$fileExtension = $data[count($data)-1];
if ($x == 0) {
$filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension;
}
else {
$filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
}
move_uploaded_file($file['userfile']['tmp_name'], $filePath);
$x++;
}
?>
试试看
只需使用我提供的功能并合并如下:
<?php
$id = 877;
$x = 0;
$_FILES = multiple($_FILES); // taken from above snippet from php.net
foreach ($_FILES as $fileKey => $file) {
$data = explode(".", $file['name']);
$fileExtension = $data[count($data)-1];
if ($x == 0) {
$filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension;
}
else {
$filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
}
move_uploaded_file($file['tmp_name'], $filePath);
$x++;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.