imatt
4 weeks ago

Laravel saving image in storage

Posted 4 weeks ago by imatt

my code was working fine but now it is not, as image is not saving in folder, i am trying to save image in storage folder, with id as image name.

Controller:

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

View: where 0000 is default image.

<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: to preview image in browser.

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

Please sign in or create an account to participate in this conversation.