Laravel在存储中保存图像

时间:2019-10-23 06:13:02

标签: ajax laravel laravel-6

我的代码工作正常,但现在却不能,因为图像未保存在文件夹中,我试图将图像保存在存储文件夹中,且ID为图像名称。

控制器:

  if($request->hasFile('profile_pic')){
        if (Input::file('profile_pic')->isValid()) {
            $file = Input::file('profile_pic');
            $destination = 'storage/app/teachers'.'/';
            $ext= $file->getClientOriginalExtension();
            $mainFilename = $teacher->id.request()->$ext;
            $file->move($destination, $mainFilename.".".'jpg');
            echo "uploaded successfully";
        }

查看:,其中0000是默认图片。

<div class="row">
 <a href="#">  <img id="imgFileUpload" name="profile_pic" src={{asset('storage/app/teachers/'.'0000'.'.jpg')}} value="{{@$teacher->id}}"
   onerror="this.src='storage/app/teachers/0000.jpg';" data-toggle="tooltip" data-placement="top" title="click to upload image" class="profile_pic rspimg shadow-sm bg-white rounded" id="imgFileUpload"   data-toggle="tooltip" data-placement="top" title="Click To Upload image"></a>
     <div class=" col-md-10">
     <input type="file"  name="profile_pic" id="FileUpload1" style="display: none" />
     <span id="spnFilePath"></span>
     </div>
</div>

ajax::在浏览器中预览图像。

<script>
    $(document).ready(function(){
$(function () {
 alert(2);
    $("#FileUpload1").change(function () {
                var imgControlName = "#imgFileUpload";
                readURL(this, imgControlName);
        });
function readURL(input, imgControlName) {
                if (input.files && input.files[0]) {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                                $(imgControlName).attr('src', e.target.result);
                        }
                        reader.readAsDataURL(input.files[0]);
                }
        }
        var fileupload = $("#FileUpload1");
        var image = $("#imgFileUpload");
        image.click(function () {
                fileupload.click();
        });

});
    });
</script>

2 个答案:

答案 0 :(得分:0)

这个$ destination ='storage / app / teachers'。'/';

尝试此$ destination = storage_path()。 '/ app / teachers'。'/';

答案 1 :(得分:0)

if ($request->hasFile('profile_pic')) {
            $image      = $request->file('profile_pic');
            $fileName   = time() . '.' . $image->getClientOriginalExtension();

            $img = Input::make($image->getRealPath());
            $img->resize(120, 120, function ($constraint) {
                $constraint->aspectRatio();                 
            });

            $img->stream(); // <-- Key point

            //dd();
            Storage::disk('local')->put('/app/teachers'.'/'.$fileName, $img, 'public');
}

尝试这个

相关问题