这是我用于上传多个文件的控制器代码,我从Google Chrome上的’postman’rest API客户端传递密钥和值.我正在从邮递员添加多个文件,但只有1个文件正在上传.
public function post_files() {
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");
foreach($_FILES['file'] as $key => $abc) {
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$filename= $temp[0];
$destinationPath = 'upload/'.$filename.'.'.$extension;
if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000)) {
if($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
if (file_exists($destinationPath)) {
echo $filename." already exists. ";
} else {
$uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
if( $uploadSuccess ) {
$document_details=Response::json(Author::insert_document_details_Call($filename,$destinationPath));
return $document_details; // or do a redirect with some message that file was uploaded
// return Redirect::to('authors')
} else {
return Response::json('error', 400);
}
}
}
}
}
我也试过这个代码,但它返回我在临时文件夹中的文件的位置
$file = Input::file('file');
echo count($file);
和
echo count($_ FILES [‘file’]);
总是回报我5.任何人都可以告诉我为什么?
为什么foreach(Input :: file(‘file’)为$key => $abc)给出错误无效的参数
解决方法:
不使用任何API,但这可能概述了原则.
我设置了这个routes.php文件,whick将帮助你进行上传测试.
routes.php文件
// save files
Route::post('upload', function(){
$files = Input::file('files');
foreach($files as $file) {
// public/uploads
$file->move('uploads/');
}
});
// Show form
Route::get('/', function()
{
echo Form::open(array('url' => 'upload', 'files'=>true));
echo Form::file('files[]', array('multiple'=>true));
echo Form::submit();
echo Form::close();
});
注意输入名称files []:如果以相同的名称上传多个文件,也包括括号.
原文地址:https://codeday.me/bug/20191004/1852958.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。