Skip to content

All topics  /  Laravel

How To Create File Object in Laravel?

Laravel

Teams applying “How To Create File Object in Laravel?” in a production project can use the official resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

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

# Laravel 6

# Laravel

# Laravel 7