Home > Backend Development > PHP Problem > What should I do if php cannot output the file?

What should I do if php cannot output the file?

藏色散人
Release: 2023-03-12 14:10:01
Original
2505 people have browsed it

Solution to the problem that php cannot output files: 1. Add "ob_clean();"; 2. Set "$pdf->Output('yourpath/output.pdf', 'F');"; 3. Set "Output($_SERVER['DOCUMENT_ROOT']".

What should I do if php cannot output the file?

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

What should I do if php cannot output the file?

Specific question:

php – TCPDF Error: Unable to create the output file

I am trying to generate pdf using a combination of TCPDF and FPDI. Here is my code.

require_once('../tcpdf/tcpdf.php');
require_once('../FPDI/fpdi.php');
$fileName = '../sample.pdf';
class PDF extends FPDI {
/**
 * "Remembers" the template id of the imported page
 */
var $_tplIdx;
var $numPages = 0;
/**
 * Draw an imported PDF logo on every page
 */
function Header() {
    global $fileName;
    if (is_null($this->_tplIdx)) {
        $this->setSourceFile($fileName);
        $this->_tplIdx = $this->importPage(1);
        $this->numPages = $this->setSourceFile($fileName);
    }
    $size = $this->useTemplate($this->_tplIdx);
}
function Footer() {
    // emtpy method body
}
}
// initiate PDF
$pdf = new PDF($fileName);
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// save file
$pdf->Output('output.pdf', 'F');
Copy after login

Here, the last line $pdf->output('output.pdf','F '); for saving the file. But it didn't work. When I only have $pdf-> Output(), it shows the pdf in the browser.

I tried $pdf->Output( 'output.pdf','D'); downloaded and it worked fine. It seems $pdf->output('output.pdf','F'); just didn't work, it showed the error TCPDF Error: Unable to create output file :output.pdf.

NOTE: There is no file permission issue

Can anyone point this out.

Solution:

Try putting ob_clean(); exactly at $pdf->output('output.pdf','F');

ob_clean();
// save file
$pdf->Output('output.pdf', 'F');
Copy after login

If that doesn't work. Than you need to set the path like this:

$pdf->Output('yourpath/output.pdf', 'F');
Copy after login

If you don’t know the absolute path, try this:

$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'output.pdf', 'F');
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if php cannot output the file?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template