laravel 文件上传

官方文档 :http://laravel-china.org/docs/5.0/requests#files
讲的太粗略了,
控制器

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;

class FileEntryController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {

        return view('file.index', compact('entries'));
    }

    public function add()
    {

        $file = Request::file('filefield');
        dump($file);
        $path = Request::file('filefield')->move("./","22.png");
        dump($path->getPathname());
        dump($path->getRealPath());
        dump($path->getExtension());
        return "ok";

    }
}
说明:
1、Request 引用的是use Illuminate\Support\Facades\Request;不是Illuminate\Http\Request,用后者会报错找不到静态方法
直接用 use Request; 也是可以的

2$file Symfony\Component\HttpFoundation\File\UploadedFile对象
[api](http://api.symfony.com/2.5/Symfony/Component/HttpFoundation/File/UploadedFile.html)
最终父类是php 内的SplFileInfo 类
路径保存php服务的tmp路径下,如"F:\xampp\tmp\php307A.tmp"

3、move() 第1个参数("/") 亲测 是在当前的盘符如E:
 ("./") 当前项目的public 路径

4、move()返回值 Symfony\Component\HttpFoundation\File\File

5、其它存储方法
  Storage::disk('local')->put($var, File::get($file));
  此时保存在项目上的 storage/app目录下

控制器

<form action="/file/add" method="post" enctype="multipart/form-data">
    <input type="file" name="filefield">
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    <input type="submit">
</form>

<h1> Pictures list</h1>

路由

Route::any('/file/index', 'FileEntryController@index');
Route::post('/file/add', 'FileEntryController@add');
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值