PDF File Reading using Laravel PHP Error : readfile($path): failed to open stream: No such file or directory-ThrowExceptions

Exception or error:
public function open()
{
    $path = '2020/March/tes.pdf';
    if (Storage::exists($path)) {
        Storage::get($path);
        // dd(Storage::files('2020\March'));
        echo readfile($path);
        //echo ini_get("open_basedir");
    } else {
        Echo ('File didnt exist');
        }
}

I’m new to web programming, here i intend to open a .pdf file in app/public/2020/March, file and path are exist, but i cant open the file, it says “readfile(2020/March/tes.pdf): failed to open stream: No such file or directory”.
I tried tutorials on the internet, linking storage, storing in app/public, caching config, clearing config, change default filesystem to public.
Thank You.

How to solve:

You are getting this error because your path is not correct. To access your file inside public folder use public_path() function

As per Laravel Documenation:

The public_path function returns the fully qualified path to the
public directory. You may also use the public_path function to
generate a fully qualified path to a given file within the public
directory

Change

 $path = '2020/March/tes.pdf';

to

$path = public_path("2020/March/tes.pdf");

Leave a Reply

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