laravel 上傳 php 需要開啓 fileinfo 擴展
前端頁面
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">上傳文件</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="file" class="col-md-4 control-label">選擇文件</label>
<div class="col-md-6">
<input id="file" type="file" name="file" >
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
上傳
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
路由
Route::any('upload', 'StudentController@upload');
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class StudentController extends Controller
{
//
public function upload(Request $request){
if($request->isMethod('POST')){
// var_dump($_FILES);
$file = $request->file('file');
if(!$file->isValid()){
//失敗提示
return redirect('upload');
}
//方法一 md5生成
// $path = $request->file('file')->store('public');
// var_dump(env('APP_URL').'/'.$path);exit();
$path = $request->file('file')->store('/public/'.date('Y-m-d').'/avatars');
$avatar = Storage::url($path);
//不可以顯示
// "http://tt001.com/public/2019-08-29/avatars/3A1sUBaCeyQF0OQ7eRgTwfAAaXgW87EqgpbEPZVZ.jpeg"
var_dump(env('APP_URL').'/'.$path);
//可以顯示
// "http://tt001.com/storage/2019-08-29/avatars/3A1sUBaCeyQF0OQ7eRgTwfAAaXgW87EqgpbEPZVZ.jpeg"
var_dump(env('APP_URL').$avatar);
exit();
//方法二md5生成
// $path = Storage::putFile('avatar', $request->file('file'));
// var_dump($path);exit();
//方法三 指定文件名
// $path = $request->file('file')->storeAs(
// 'avatars', 'newFileName'
// );
// var_dump($path);exit();
//方法四 指定磁盤 指定路徑 文件名隨機
$path = $request->file('file')->store(
'avatars', 'public'
);
var_dump($path);exit();
//方法五
// 原文件名 20181207140247195.jpg
$originalName = $file->getClientOriginalName();
// 擴展名 jpg
$ext = $file->getClientOriginalExtension();
// MimeType image/jpeg
$type = $file->getClientMimeType();
// 臨時絕對路徑 C:\Windows\phpFBC1.tmp
$realPath = $file->getRealPath();
$filename = date("ymdhis").'-'.uniqid().'.'.$ext;
$bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
var_dump($bool);
exit;
return redirect('upload');
}
//http://tt001.com/storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg
echo asset('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');
echo "<br />";
//D:\phpStudy\PHPTutorial\WWW\blog88\storage\storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg
echo storage_path('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');
return view('student.upload');
}
}
添加磁盤
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "s3", "rackspace"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'uploads' => [
'driver' => 'local',
// 'root' => storage_path('app/uploads'),
'root' => public_path('uploads'),
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
];
文件 URL
使用 local 或 s3 驅動時,可以使用 url 方法獲取給定文件的 URI。如果你使用的是 local 驅動,通常會在給定路徑前加上 /storage,並返回該文件的相對 URL;如果使用的是 s3 或 rackspace 驅動,則會返回完整的遠程 URL:
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file1.jpg');
注:記住,如果你在使用 local 驅動,所有需要公開訪問的文件都應該存放在 storage/app/public 目錄下,此外,你還需要創建一個指向 storage/app/public 目錄的軟鏈接 public/storage。
echo asset('storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg');
http://tt001.com/storage/QU9oENBf7t6uV1FzD72pm0o1UJrHn1P0y0ewTghQ.jpeg