Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 getClientOriginalExtension() returns empty string

Tags:

laravel

I checked if file exists using Input::hasFile('File'), and it returns true and successfully got file's binary. But Input::file('File')->getClientOriginalExtension() returns empty string.

Here's my controller

    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;
        }

    }

And below code is Front-end Ajax code.

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]);
                        }
                    },
                });

I can't find where is bug code and mistake I made. Please help me. This make me mad.

like image 983
OhHyeonJun Avatar asked Feb 06 '23 11:02

OhHyeonJun


1 Answers

The getClientOriginalExtension method returns the extension of the actual file uploaded some-image.pdf, this is not considered a safe value. Instead you could best use guessExtension.

The guessExtension method uses the actual mime type and returns the related file type.

like image 69
Luceos Avatar answered Feb 08 '23 16:02

Luceos