如何修复'file_get_content'无法打开流

时间:2019-06-30 09:30:20

标签: php laravel

我正在使用cups-ipp从Web项目中打印

我得到了这个错误

  

file_get_contents(./ helloworld.pdf):无法打开流:否这样   文件或目录

我试图从存储中获取文件,但是出现了相同的错误

        $builder = new Builder();

        $responseParser = new ResponseParser();

        $printerManager = new PrinterManager($builder, $client, $responseParser);
        $printer = $printerManager->findByUri('ipp://localhost:631/printers/HP_HP_ColorLaserJet_MFP_M278-M281');

        $jobManager = new JobManager($builder, $client, $responseParser);
        $filePath = '../../../storage/app/public/estimated_dates.pdf';

        $job = new Job();
        $job->setName('job create file');
        $job->setUsername('demo');
        $job->setCopies(1);
        $job->setPageRanges('1');
        $job->addFile('./helloworld.pdf');
        $job->addAttribute('media', 'A4');
        $job->addAttribute('fit-to-page', true);
        $result = $jobManager->send($printer, $job);

我已链接存储,并且确定路径正确(我也使用过auto complete path plugin

1 个答案:

答案 0 :(得分:0)

尝试利用Laravel的base_path()帮助器。

base_path()等于项目的根目录。因此,例如,您要访问estimated_dates.pdf。正确的解决方案是:

base_path('storage/app/public/estimated_dates.pdf');

但是,您可以利用:

php artisan storage:link

,您可以通过以下方式访问文件:asset('estimated_dates.pdf')

我猜您的helloworld.pdf位于应用程序的根目录中,所以:

base_path('helloworld.pdf')
  

base_path():https://laravel.com/docs/5.8/helpers#method-base-path

     

存储:链接:https://laravel.com/docs/5.8/filesystem#configuration