How to get all the contents of a file in Laravel

Question:

How to get all the contents of a file in Laravel

Solution:


First Use Laravel File Facades function Use File at top of the class. Like,
use File;

Then
File::get($file_name);

Real Life example of determining if a file is in that folder and then get the content of that file
$file_name = 'images/students/1200.jpg';
if(File::exists($file_name))
{
    $contents = File::get($file_name);
}


Now see what's in the backend ? Laravel Core function of get() is -
    /**
     * Get the contents of a file.
     *
     * @param  string  $path
     * @param  bool  $lock
     * @return string
     *
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function get($path, $lock = false)
    {
        if ($this->isFile($path)) {
            return $lock ? $this->sharedGet($path) : file_get_contents($path);
        }

        throw new FileNotFoundException("File does not exist at path {$path}");
    }

So, Laravel get() function use it's own is_file() function first and then throws an exception if there is no file in that directory.


So, we can now easily get all of the file contents of any file. That may be a jpg, png, pdf or any kinds of file. It's a pretty one line function.



Tags:
How to get all the contents of a file in Laravel, Determine if file exists in a directory or in a path in Laravel, Laravel if file is esisted in the directory, Laravel get all contents of a file, get the file content in Laravel, Laravel files,
How to get all the contents of a file in Laravel How to get all the contents of a file in Laravel Reviewed by Maniruzzaman Akash on October 13, 2017 Rating: 5

1 comment:

  1. Our certified Laravel developers can create scintillating extensions and mapping of web applications that ensure quicker performance. We have the acumen of delivering small scale to large scale projects based on laravel Development. You name the service that you are looking for in Laravel technology and we will be able to deliver that to you in minimal time. Contact@ExpressTechSoftwares.Com or +91-9806724185

    ReplyDelete

Powered by Blogger.