StephRocks22's avatar

In regards to uploading file in laravel

Hi everyone, hope all is well. I am having this problem. I have created a form where you can upload video files, but I notice that when uploading it, video takes time to upload depending on the size of the file,so to avoid people staring at a screen for a long time waiting for their video to upload I saw this example on the this website about creating a progress bar. I tried it out and yes its working, like it saves to the public app folder and also it saves to my aws3 bucket folder as I requested but I noticed that its not saving the file in the database and I don't know why it by passes the database. here is my code below

//fileUpload.blade.php

 <div class="container">
    <div class="card">
      <div class="card-header">
      </div>
      <div class="card-body">
            <form method="POST" action="{{ route('fileUploadPost') }}" enctype="multipart/form-data">
                @csrf
                <div class="form-group">
                    <input name="file" id="poster" type="file" class="form-control"><br/>
                    <div class="progress">
                        <div class="bar"></div >
                        <div class="percent">0%</div >
                    </div>
                    <input type="submit"  value="Submit" class="btn btn-success">
                </div>
            </form>    
      </div>
    </div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
 
<script type="text/javascript">
 
    function validate(formData, jqForm, options) {
        var form = jqForm[0];
        if (!form.file.value) {
            alert('File not found');
            return false;
        }
    }
 
    (function() {
 
    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');
 
    $('form').ajaxForm({
        beforeSubmit: validate,
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            var posterValue = $('input[name=file]').fieldValue();
            bar.width(percentVal)
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        success: function() {
            var percentVal = 'Wait, Saving';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        complete: function(xhr) {
            status.html(xhr.responseText);
            alert('Uploaded Successfully');
            window.location.href = "file-upload";
        }
    });
     
    })();
</script>

//file-upload controller

 public function fileUploadPost(Request $request)
    {
        $request->validate([
            'file' => 'required',
        ]);
 
       


  $my_video=new Video;      
 $fileName = time().'.'.request()->file->getClientOriginalName();
 
        request()->file->move(public_path('/app'), $fileName);
            $name_file=uniqid().'video.mp4';
         $fmpeg=FFMpeg::fromDisk('local')
         ->open($fileName)
    ->addFilter(function ($filters) {
        $filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
    })
    ->export()
    ->toDisk('s3')
    ->inFormat(new \FFMpeg\Format\Video\X264('libmp3lame'))
    ->save($name_file);
               $imageName = Storage::disk('s3')->url($name_file);


    $my_video->title=$imageName;
    $my_video->save();

        return response()->json(['success'=>'You have successfully upload file.']);
    }
}
0 likes
6 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Are you sure that Storage::disk(... is returning the filename?

Are there any errors in the laravel log?

Do you get success message?

1 like
StephRocks22's avatar

@SNAPEY - @snapey I don't see any errors. the files goes to the public/app folder successfully and it saves to my aws3 bucket successfully. I don't know whats going on

peterl38's avatar

What are you expecting to be written to the DB, just the filename?

I maybe missing it but I can see nowhere that would write to the DB in the code you show, I would have thought it would be in the controller?

StephRocks22's avatar

@snapey @peterl38 I got it. There was an error, I looked in the laravel log and the problem was that I didn't use storage in the controller that's why when I did this

  $imageName = Storage::disk('s3')->url($name_file);


    $my_video->title=$imageName;
    $my_video->save();

the s3 url was'nt going to the database because I wasn't using Storage

Snapey's avatar

two of my suggestions were correct then? ;-)

Please or to participate in this conversation.