How To Create File Object in Laravel?

Laravel 6 , Laravel , Laravel 7

Nicesnippets

497

19-06-2020


Hi Dev,

I am going to learn you how to create file object in laravel.We will create file object in laravel application.in 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 :

<?php

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(

$newUrl,

$path_parts['basename'],

$imgInfo['mime'],

filesize($url),

TRUE,

);

return $file;

}

public function getFileObject()

{

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

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

dd($files);

}

Output :

It will help you...


Recommended Posts