我想将图片上传到服务器。创建视图页面是:
{!! Form::open(['url'=>'imagesupload','enctype'=>'multipart/form-data','method' => 'post']) !!}
{!! Form::file('image') !!}
{!! Form::submit('Submit',['class'=>'btn btn-primary form-control']) !!}
{!! Form::close() !!}
我的控制器代码是:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
public function store(Request $request)
{
dd($request['image']);
}
我的路线是:
Route::post('imagesupload','ImagesController@store');
上传图片时的页面:
UploadedFile {#208 ▼
-test: false
-originalName: "WIN_20160626_17_24_33_Pro.jpg"
-mimeType: "application/octet-stream"
-size: 0
-error: 1
path: ""
filename: ""
basename: ""
pathname: ""
extension: ""
realPath: "/var/www/html/public"
aTime: 1970-01-01 08:00:00
mTime: 1970-01-01 08:00:00
cTime: 1970-01-01 08:00:00
inode: false
size: false
perms: 00
owner: false
group: false
type: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
有错误。图像没有大小。 希望有人能告诉我原因。谢谢!
答案 0 :(得分:0)
我认为你遗漏了一些东西。你必须使用$ request->文件(' fieldname')才能访问文件。 尝试dd($ request->文件('图像'));
答案 1 :(得分:0)
在视图中
{!! Form::open(array('url'=>'imagesupload','method'=>'POST', 'files'=>true)) !!}
{!! Form::file('image') !!}
<p class="errors">
{!!$errors->first('image')!!}</p>
@if(Session::has('error'))
<p class="errors">{!! Session::get('error') !!}</p>
@endif
<div id="success"> </div>
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
控制器中的
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Input;
use Validator;
use Redirect;
use Session;
public function store() {
// getting all of the post data
$file = array('image' => Input::file('image'));
// return $file;
// setting up rules
$rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
return Redirect::to('uploadprofilepicture')->withInput()->withErrors($validator);
}
else {
// checking file is valid.
if (Input::file('image')->isValid()) {
$destinationPath = 'images/profile/'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension;
// renameing image
// return $fileName;
Input::file('image')->move($destinationPath, $fileName);
// uploading file to given path
// sending back with message
Session::flash('success', 'Upload successfully');
}
else {
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('yourviewfile');
}
}
}
答案 2 :(得分:0)
创建一个包含属性'files'=&gt; true的表单,以便上传文件
{!! Form::open(array('url'=>'imagesupload','method'=>'POST', 'files'=>true)) !!}
在请求中,您可以通过$request->filename