laravel 在laravel中上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39239049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload file in laravel
提问by Davide Minelli
I guys I read the documentation for do a upload file in laravel... But I don't understand more it (I'm beginner) It's my image.blade.php
伙计们,我阅读了在 laravel 中上传文件的文档......但我不明白(我是初学者)这是我的 image.blade.php
{{Form::open(['url'=>'administrator/store ', 'files' => true])}}
{!!Form::file('image') !! }
{!! Form::submit('next')!!}
{{Form::close()}}
Administrator Controller
管理员控制器
use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
public function store(Request $request) {
Storage::put($request->image, 'test') ;
}
I don't understand what I will put into the function..... Help me pls... Greetings!
我不明白我将在函数中放入什么..... 请帮帮我... 问候!
回答by felipsmartins
\Illuminate\Http\Request::file()
is what you have when you're uploading files.
\Illuminate\Http\Request::file()
是您上传文件时所拥有的。
This is just a instance of \Symfony\Component\HttpFoundation\File\UploadedFileclass so you can movefile to destination/storage what you want, something like that:
这只是\Symfony\Component\HttpFoundation\File\UploadedFile类的一个实例,因此您可以将文件移动到目标/存储您想要的位置,如下所示:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MainController extends Controller
{
public function upload(Request $request)
{
/**
* @var Symfony\Component\HttpFoundation\File\UploadedFile
*/
$uploadedFile = $request->file('image');
if ($uploadedFile->isValid()) {
$uploadedFile->move(destinationPath, $fileName);
}
}
}
Aso, you've been used \Illuminate\Filesystem\Filesystem::put()
in a wrong way. Below is like that method is implemented:
阿苏,你被人用\Illuminate\Filesystem\Filesystem::put()
错了。下面是该方法的实现:
/**
* Write the contents of a file.
*
* @param string $path
* @param string $contents
* @param bool $lock
* @return int
*/
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}