0

I have hosted my laravel application on shared server where public directory is public_html. The order invoice path should be /var/www/html/public_html/user_invoices/invoice_order_447B11621531373227.pdf

  • Laravel version: 5.7
  • Dompdf: "barryvdh/laravel-dompdf": "^0.9.0"

I am getting this error:

ErrorException: file_put_contents(/var/www/html/myproject/public_html/user_invoices/invoice_order_447B11621531373227.pdf): failed to open stream: No such file or directory in /var/www/html/crm/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:122

This is my mailable class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use PDF;

class UserNotificationsOrderPlaced extends Mailable
{
    public $data;
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {           

        $path = public_path('user_invoices/invoice_order_'.$this->data[0]->order_id.'.pdf');            
        $pdf = PDF::loadView('pdf.invoice', ['order'=>$this->data[0]])->save($path);        
        return $this->view('emails.orders')->with(['orders'=>$this->data])->attach($path);

            
    }
}

AppServiceProvider:

I have tried both solutions realpath and base_path but still getting same error:

return realpath(base_path().'/../public_html');
return base_path().'/../public_html';

return realpath(base_path().'/public_html');
return base_path().'/public_html';

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //Schema::defaultStringLength(191);
        if (class_exists('Swift_Preferences')) {
            \Swift_Preferences::getInstance()->setTempDir(storage_path().'/tmp');
        } else {
            \Log::warning('Class Swift_Preferences does not exists');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
  public function register()
  {
        $this->app->bind('path.public', function() {
            return realpath(base_path().'/../public_html');    
            //return base_path().'/../public_html';
        });
  }
}
Hemant Kumar
  • 1,025
  • 15
  • 32
  • 1
    Have you tried this? $this->app->bind('path.public', function() { return base_path('public_html'); }); – GlennM May 21 '21 at 06:31
  • 1
    You shouldn't change the public path because of your server's folder structure. Go to the public_html/index.php and update the paths of required files according to the application path. Everything else will work normally. – Bulent May 21 '21 at 06:40
  • Move your application files from `/var/www/html/myproject` to `/var/www/html` and then also check to make sure the `config/filesystems.php` refers to the correct public path – apokryfos May 21 '21 at 06:52
  • What about changing `config('filesystems.disks.public.root');`? – shaedrich May 21 '21 at 07:13
  • 1
    Does this answer your question? [How to point laravel project to a public folder under public\_html in shared hosting](https://stackoverflow.com/questions/55094509/how-to-point-laravel-project-to-a-public-folder-under-public-html-in-shared-host) – Peppermintology May 21 '21 at 07:18

2 Answers2

0

You need to create a route to clear cache on server

Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
    $exitCode = Artisan::call('config:cache');
    return 'DONE'; //Return anything
});
Raja
  • 1
  • 2
0

Solved:

I have manually deleted everything inside cache but it did not worked. This helps me to resolve the issue.

Laravel showing "Failed to clear cache. Make sure you have the appropriate permissions"

Finally php artisan cache:clear works with above solution.

Hemant Kumar
  • 1,025
  • 15
  • 32