php – 在Laravel 4中上传多个文件

这是我用于上传多个文件的控制器代码,我从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 []:如果以相同的名称上传多个文件,也包括括号.

相关文章

laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载