文件上传
在config文件夹下新建一个 项目名.php
return [
'title' => 'My Test',
'posts_per_page' => 5,
'uploads' => [
'storage' => 'local',
'webpath' => '/uploads', //上传文件存放的目录 在public文件夹下
],
];
view视图代码
{{--如果要上传文件,必须要在Form里面设置files为true--}}
{!! Form::open(array('url'=>'create/show','files'=>true)) !!}
{{--name为iamge--}}
{!! Form::file('image') !!}
<br />
{!! Form::submit('提交') !!}
{!! Form::close() !!}
<?php
namespace App\Http\Controllers;
//必须使用这个Request
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use App\Test;
class CreateController extends Controller
{
public function create(){
return view('create.create');
}
public function show(Request $request){
if ($image = $request->hasFile('image')){
$file = $request->file('image');
//判断文件上传过程中是否出错
if(!$file->isValid()){
return '文件上传出错!';
}
//或者文件夹路径 如果没有则返回false
$destPath = realpath(public_path('uploads'));
/**
* 检查一个文件或目录是否存在
* @param 文件或文件夹路径
* @return
*/
if(!file_exists($destPath)){
/**
* 试图创建指定的目录的路径名.
* @param string $pathname 文件或文件夹所在路径.
* @param int $mode [optional] 默认情况下,默认的模式为0777(rwxrwxrwx)
* 读、写、运行三项权限可以用数字表示,就是r=4,w=2,x=1
* r(Read,读取) w(Write,写入) x(eXecute,执行)
* @param bool $recursive [optional] <p>
* 允许嵌套目录中指定的路径创造。默认为假
* @param resource $context [optional] ¬e.context-support;
* @return bool true on success or false on failure.
*/
mkdir($destPath,0755,true);
}
/**
* 返回原始文件名.
* 这是提取请求的文件已经上传
* @return string|null 原来的名字
*/
$filename = $file->getClientOriginalName();
/**
* 将文件移动到一个新的位置.
* @param string $directory 字符串目录目标文件夹
* @param string $name 字符串名称的新文件名
* @return 返回文件,一个表示新文件的文件对象
* @throws FileException if, 如果因为任何原因,文件不能被移动
*/
if(!$file->move($destPath,$filename)){
return '保存文件失败!';
}
return '文件上传成功!';
}
return '创建成功';
}
}
下载
public function down()
{
return response()->download(realpath(base_path('public/uploads')).'/logo.png',
'Laraveldown.png');
}