Laravel 5 getClientOriginalExtension()返回空字符串

时间:2016-12-22 09:00:51

标签: laravel

我使用Input::hasFile('File')检查文件是否存在,并返回true并成功获取文件的二进制文件。 但Input::file('File')->getClientOriginalExtension()返回空字符串。

这是我的控制器

    public function ConfirmWrite()
    {
        if (!Session::has('id')) {
            return '0:Please log in.';
        }
        $Data = Request::all();
        $Data['uid'] = Session::get('id');
        $Data['mid'] = 0;
        var_dump(Input::file('File')->getClientOriginalExtension());
        return '1';
        if (Input::hasFile('File')) {
            $file = Input::file('File');

            $rules = ['File' => 'mimes:jpeg,bmp,png,jpg|max:10000'];

            $validator = Validator::make(['File' => $file], $rules);
            if ($validator->fails()) {
                return '0:Check your File.';
            }
            $Data['Thumbnail'] = $file->getClientOriginalExtension();
            $destinationPath = 'images/post/thumbnail/';
            $Content = Post::SaveContent($Data);
            if($Data['Share'] == 'true'){
                $fb = FacebookHelper::WithToken(Session::get('FbToken'));
                $Link = URL::to('/post').'/'.$Content;
                $fb->ShareLink($Link);
            }
            $upload_success = $file->move($destinationPath, $Content . '.' . $Data['Thumbnail']);
            echo "asdfasdfasdf : ".$Data['Thumbnail'];
            if ($upload_success) {
                UsefulHelper::ImageResizing($destinationPath, $Content . '.' . $Data['Thumbnail'], 320, 'small');
                UsefulHelper::ImageResizing($destinationPath, $Content . '.' . $Data['Thumbnail'], 700, 'medium');
                UsefulHelper::ImageResizing($destinationPath, $Content . '.' . $Data['Thumbnail'], 1920, '');
                foreach (explode(',', $Data['Tag']) as $tag) {
                    HashTag::SaveHashTag($tag, 'post', $Content);
                }
                return '1:' . $Content;
            } else {
                return '0:Somethings wrong';
            }
        } else {
            $Content = Post::SaveContent($Data);
            if($Data['Share'] == 'true'){
                $fb = FacebookHelper::WithToken(Session::get('FbToken'));
                $Link = URL::to('/post').'/'.$Content;
                $fb->ShareLink($Link);
            }
            foreach (explode(',', $Data['Tag']) as $tag) {
                HashTag::SaveHashTag($tag, 'post', $Content);
            }
            return '1:' . $Content;
        }

    }

以下代码是前端Ajax代码。

var fData = new FormData;
                GlobalVar.Thumbnail == '' ? '' : fData.append('File', DataURLtoBlob(GlobalVar.Thumbnail));
                fData.append('Title', $('.contents-details').find('h1').html());
                fData.append('Subtitle', $('.contents-details').find('h2').html());
                fData.append('Content', $('#post-editor').froalaEditor('html.get'));
                fData.append('Align', EditorAlign);
                fData.append('Tag', Tag);
                fData.append('Share',GlobalVar.FBShare);
                $.ajax({
                    url: '{{ URL::to('/post/write') }}',
                    type: 'post',
                    processData: false,
                    enctype: "multipart/form-data",
                    contentType: false,
                    cache: false,
                    data: fData,
                    headers: {
                        'X-CSRF-Token': '{{ csrf_token() }}',
                    },
                    success: function (result) {
                        var Check = $.trim(result).split(':');
                        $('.submit-loading').css('display', 'none');
                        if (Check[0] == '1') {
                            checkUnload = false;
                            location.href = '{{ URL::to('post') }}/' + Check[1];
                        } else {
                            console.log(result);
                            Warning(Check[1]);
                        }
                    },
                });

我无法找到错误代码和我犯的错误。请帮我。这让我很生气。

1 个答案:

答案 0 :(得分:6)

getClientOriginalExtension方法返回上传的some-image.pdf实际文件的扩展名,这不被视为安全值。相反,您最好使用guessExtension

guessExtension方法使用实际的mime类型并返回相关的文件类型。