How To Create File Object in Laravel?

Admin   Laravel   483  2021-03-16 00:30:10

Hi Dev,

I am going to learn you how to create file object in laravel.We will create file object in laravel this article I will show you create file object in laravel app.

It is easy and simply to create file object in laravel app.In this solution I will use public_path()function to get file path.

Create file object in laravel and use Symfony\Component\HttpFoundation\File\UploadedFile;.

Here I will create object for demo.jpg file availabe in public/image directory. I will give you solution for laravel create file object.So Let's see the solution.

Solution :



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class HomeController extends Controller


public function getFile($url)


//get name file by url and save in object-file

$path_parts = pathinfo($url);

//get image info (mime, size in pixel, size in bits)

$newPath = $path_parts['dirname'] . '/tmp-files/';

if(!is_dir ($newPath)){

mkdir($newPath, 0777);


$newUrl = $newPath . $path_parts['basename'];

copy($url, $newUrl);

$imgInfo = getimagesize($newUrl);

$file = new UploadedFile(







return $file;


public function getFileObject()


$url = public_path('image/demo.jpg');

$files = $this->getFile($url);



Output :

It will help you...