【问题标题】:Laravel5.2 upload image errorLaravel5.2上传图片报错
【发布时间】:2016-08-20 14:30:43
【问题描述】:

我想上传图片到服务器。创建视图页面是:

{!! 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
}

有一个错误。图像没有大小。 希望有人能告诉我为什么。谢谢!

【问题讨论】:

    标签: image laravel upload


    【解决方案1】:

    我认为你遗漏了一些东西。你必须使用 $request->file('fieldname') 才能访问文件。 试试 dd($request->file('image'));

    【讨论】:

    • 我试过了。一样的。 Input::file('image') 可以工作。可能是请求不适合发布文件。我不知道。
    【解决方案2】:

    在视图中

       {!! 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');
     }
    }
    }
    

    【讨论】:

    • 使用 Input::file('image');而不是 dd($request['image']);
    • 您认为我的答案正确吗?请问你可以吗?
    【解决方案3】:

    创建一个带有属性'files'=>true的表单以上传文件

    {!! Form::open(array('url'=>'imagesupload','method'=>'POST', 'files'=>true)) !!}
    

    您可以通过$request-&gt;filename获取它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-11
      • 2021-01-24
      • 2019-10-03
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多