Laravel Renaming Uploaded Files Automatically

Today, We want to share with you Laravel Renaming Uploaded Files Automatically.In this post we will show you Renaming uploaded file before download, hear for Rename a file before moving it to Directory we will give you demo and example for implement.In this post, we will learn about Laravel : To rename an uploaded file automatically with an example.

Laravel Renaming Uploaded Files Automatically

There are the Following The simple About Laravel Renaming Uploaded Files Automatically Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to Renaming upload files in Laravel, so the Renaming upload files in Laravel 5.7 for this example is following below.

Read Also:  Laravel 5.8 Merge Multiple Collection Paginate

Rename a file before moving it to Directory

public function upload(Request $request)
{
  $file = $request->file('file');
  $orignal_filename = time().$file->getClientOriginalName();

  Storage::disk('local')->putFileAs(
    'files/'.$orignal_filename,
    $file,
    $orignal_filename
  );

  $fl_upload = new Upload;
  $fl_upload->orignal_filename = $orignal_filename;

  $fl_upload->user()->associate(auth()->user());

  $fl_upload->save();

  return response()->json([
    'id' => $fl_upload->id
  ]);
}

Example of File Upload with Validation in Laravel 5.7

resources/views/uploads.blade.php

Create a view file where user can upload files : Create Route, Controller and View for Form Upload Page.

@extends('app')
@section('content')
 
    @if (Session::has("message"))
       {{ Session::get("message") }}
     @endif
     <hr />
 
    <form action="add" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
 
@endsection

Laravel Route and Controller Method to Upload File

app/Http/Controllers/ProfileController.php

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Input;
use Validator;
use Request;
use Response;
 
 
class ProfileController extends Controller {
 
    public function index() {
        return view('uploads');
    }
 
    public function uploadFiles() {
 
        $input = Input::all();
        
        $rules = array(
            'file' => 'image|max:5000',
        );
    
        $validation = Validator::make($input, $rules);
 
        if ($validation->fails()) {
            return Redirect::to('/')->with('message', $validation->errors->first());
        }
        
        
           $file = array_get($input,'file');
            $destinationPath = 'images/uploads'; 
            $extension = $file->getClientOriginalExtension(); 
            $fileName = rand(11111, 99999) . '.' . $extension; 
            $response_success = $file->move($destinationPath, $fileName); 
        
        if ($response_success) {
            return Redirect::to('/')->with('message', 'Files/Image uploaded successfully');
        } 
    }
 
}

app/https/web.php

Route::get('/', '[email protected]');
Route::post('upload/add', '[email protected]');

Laravel Example of File Upload

 /** Example of File Upload */
    public function uploadFilePost(Request $request){
        $request->validate([
            'fileToUpload' => 'required|file|max:1024',
        ]);

        $fileName = "fileName".time().'.'.request()->fileToUpload->getClientOriginalExtension();

        $request->fileToUpload->storeAs('logos',$fileName);

        return back()
            ->with('success','You have successfully upload files/image.');

    }

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Check if the Uploaded File is an Image in Laravel 6

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.


I hope you get an idea about Laravel Renaming Uploaded Files Automatically.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.