【问题标题】:Laravel saving image in storageLaravel 在存储中保存图像
【发布时间】:2019-10-23 06:13:02
【问题描述】:

我的代码工作正常,但现在不行,因为图像没有保存在文件夹中,我正在尝试将图像保存在存储文件夹中,以 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>

【问题讨论】:

  • 错误是什么?
  • 图像未存储。
  • 您在上传图片时是否遇到任何错误?意味着你得到回声“上传成功”;作为回应?
  • 我在屏幕上没有收到任何错误,只是没有保存。

标签: ajax laravel laravel-6


【解决方案1】:

取而代之的是 $destination = 'storage/app/teachers'.'/';

试试这个 $destination = storage_path() 。 '/app/teachers'.'/';

【讨论】:

    【解决方案2】:
    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');
    }
    

    试试这个

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-11
      • 1970-01-01
      • 2020-10-08
      相关资源
      最近更新 更多