Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ranmouri's avatar

How To Fix Error file_get_contents(/home/myapp/laravel/public/invoice.rtf): failed to open stream: No such file or directory?

I use Laravel for my website. The website is already running online using shared hosting. Everything went smoothly. Except for 1 thing that still an error occurs.

I am using Novay WordTemplate to export to word dynamically.

When I run it on localhost the WordTemplate runs fine. But when I uploaded it on the hosting, an error occurred when I called mywebsite.com/word. And the route:

Route::get('/word', [DataController::class, 'word'])->name('word');

The error message that appears is :

file_get_contents(/home/mywebsite/laravel/public/certificate.rtf): failed to open stream: No such file or directory

I put the certificate.rtf file in the public_html folder. And the public_html folder is outside the laravel folder.

Here is the contents of my Controller:

public function word()
    {
        $file = public_path('certificate.rtf');
		            
		$array = array(
                '[NUMBER]' => '015/BT/SK/V/2017',
                '[COMPANY]' => 'CV. Borneo Teknomedia',
                '[NAME]' => 'Melani Malik',
                '[ID_COMPANY]' => '6472065508XXXX',
                '[ADDRESS]' => 'Jl. Manunggal Gg. 8 Loa Bakung, Samarinda',
                '[ABOUT]' => 'Application for managing Personal Identity',
                '[CITY]' => 'JAKARTA',
                '[DIRECTOR]' => 'Johny Deep',
                '[DATE]' => date('d F Y'),
            );

   		$nama_file = 'Certificate.doc';
		
		return redirect()->back()->with(WordTemplate::export($file, $array, $nama_file));
    }

Supposedly when I open mywebsite.com/word, a Certificate.doc file will be downloaded according to the data on the Controller. When I tried it on localhost, it was running fine. But when I upload it on the hosting why does an error occur?

Please help me solve the problem.

0 likes
7 replies
Sinnbeck's avatar

Well it sounds like the file certificate.rtf does not exist in your public directory on the shared host. Simple as that

1 like
ranmouri's avatar

There is a certificate.rtf in my public_html folder. But public_html is outside the laravel folder.

The error message that appears is :

file_get_contents(/home/mywebsite/laravel/public/certificate.rtf): failed to open stream: No such file or directory

The location of certificate.rtf should be in : /home/mywebsite/public_html/certificate.rtf. Without laravel and public replaced with public_html.

Please help me solve the problem.

Tray2's avatar

@ranmouri

You do this in your code

$file = public_path('certificate.rtf');

That translates to the public directory of your project.

1 like
ranmouri's avatar

@Sinnbeck @tray2 I added the following code in the AppServiceProvider:

public function register()
    {
        $this->app->bind('path.public', function() {
            return realpath(base_path().'/../public_html');
          });
    }

After I added the code, there was no error like before. But a new problem arose. When I open mywebsite.com/word it should automatically download the Certificate.doc file. But this time it appears like the picture: ibb(dot)co/h89RX0w . replace (dot) with .

Please help me solve the problem.

Tray2's avatar

@ranmouri Why can't you just place the file in the storage folder where it is supposed to be place?

JerryWS95's avatar

I got same problems when the source code uploaded on hosting, but it already fixed, after the RTF file loaded u need change the line of WordTemplate.php in vendor novay\laravel-word-template to download the file doc

header("Content-Disposition: attachment; filename={$filename}"); 

instead this

header("Content-disposition: inline; filename={$filename}");

Please or to participate in this conversation.