Laravel Renaming Uploaded Files Automatically – Technology

Laravel Renaming Uploaded Files Automatically – Technology

In this post we will give you information about Laravel Renaming Uploaded Files Automatically – Technology. Hear we will give you detail about Laravel Renaming Uploaded Files Automatically – TechnologyAnd how to use it also give you demo for it if it is necessary.

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.

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

Another must read:  Removing # hash from URL in AngularJS

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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 AppHttpControllers;
 
use AppHttpControllersController;
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 :

Another must read:  laravel Redirect jQuery ajax success

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

Hope this code and post will helped you for implement Laravel Renaming Uploaded Files Automatically – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  26  =  28

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US