Search code examples
phppdffpdf

FPDF Did not save PDF in server


Goodafternoon, I made this script but I can't get where I'm wrong, it didn't save the PDF as I tried to program. Thank you if there's any suggestion. I know the problem is in the logic inside: function file_newname but I don't know where exactly. Thank you in advance.

    <?php
require('fpdf.php');
class PDF extends FPDF{
    function Header(){
        $this->Image('img/oet.png',10,6,30);
        $this->SetFont('Helvetica','B',25);
        $this->Cell(55);
        $this->Cell(100,10,"TITLE",0,0,'C');
        $this->SetFont('Helvetica','B',18);
        $this->Ln(10);
        $this->Cell(55);
        $this->Cell(100,10,"SUBTITLE",0,0,'C');
        $this->Ln(20);
    }
    function Footer(){
        $this->SetY(-15);
        $this->SetFont('Times','I',8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
    function file_newname($filename){
        if($pos = strrpos($filename, '.')) {
            $name = substr($filename, 0, $pos);
            $ext = substr($filename, $pos);
        }else{
            $name = $filename;
        }
        $newpath = 'docs/'.$filename;
        $counter = 0;
        while (file_exists("docs/")) {
            $filename = $name .'_'. $counter . $ext;
            $newpath = "docs/".$filename;
            $counter++;
        }
        $this->Output("docs/".$filename);
    }
}


$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=10;$i++){
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
}
$pdf->Output();
$pdf->file_newname(date("Ymd").".pdf");
?>

Solution

  • Here is the right solution:

    function file_newname($filename){
            if($pos = strrpos($filename, '.')) {
                $name = substr($filename, 0, $pos);
                $ext = substr($filename, $pos);
            }else{
                $name = $filename;
            }
            $newpath = 'documenti/'.$filename;
            $counter = 0;
            while (file_exists("documenti/".$filename)) {
                $filename = $name .'_'. $counter . $ext;
                $newpath = "documenti/".$newname;
                $counter++;
            }
            $this->Output("documenti/".$filename);
            $this->Output();
        }
    $pdf->file_newname(date("Ymd").".pdf");<<<<