laravel不能“move()”文件

时间:2016-11-05 12:44:58

标签: php ajax laravel

我正在使用Laravel 5.3 我使用cropper https://github.com/fengyuanchen/cropper裁剪图片,然后通过ajax将其上传到后端。

$('#uploadAvatar').on('click', function (e) {

    $("#image").cropper('getCroppedCanvas').toBlob(function (blob) {
        var formData = new FormData();

        formData.append('croppedImage', blob);

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            type: "POST",
            url: "{{ url('/change-avatar') }}",
            processData: false,
            contentType: false,
            data: formData
        }).done(function (response) {
            //...
        }).fail(function (data) {
            //...
        });
    });
});

ajax请求的标头是这样的: enter image description here

Laravel获得图像并执行以下操作:

public function changeAvatar (Request $request)
{
    $file = $request->croppedImage;
    $input = array('image' => $file);
    $rules = array(
        'image' => 'image'
    );
    $validator = \Validator::make($input, $rules);
    if ( $validator->fails() ) {
        return \Response::json([
            'success' => false,
            'errors' => $validator->getMessageBag()->toArray()
        ]);

    }

    $destinationPath = '/images/uploads/';
    $user=\Auth::user();
    $fileName = $user->id . '_' . time() . '.'.$file->clientExtension();
    $file->move($destinationPath, $fileName);
    //dd($file);
    $user ->avatar = $destinationPath.$fileName;
    $user ->save();

    return \Response::json([
        'success'=>true,
        'avatar'=>asset($destinationPath.$fileName),
    ]);

}

在上述方法中,行$file->move($destinationPath, $fileName);无法正常工作 为什么?

0 个答案:

没有答案