2015-10-30 67 views
0

Goodafternoon,我做了這個腳本,但我不能得到我錯的地方,它沒有保存PDF,因爲我試圖編程。 謝謝,如果有任何建議。 我知道問題出在邏輯裏面:函數file_newname但我不知道在哪裏。 預先感謝您。FPDF沒有保存PDF在服務器

<?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"); 
?> 
+0

'$這 - >輸出(「文檔/".$ NEWNAME);''..目錄docs'有?? –

+0

是的,「docs /」是正確的 –

回答

0

這裏是合適的解決方案:

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");<<<< 
0

您嘗試輸出後正在刪除您的新文件名。使用這個:

$filename = $pdf->file_newname(date("Ymd").".pdf"); 
$pdf->Output($filename, 'F'); 

不記得F標誌在輸出命令中做了什麼,但這就是我的腳本。

+0

不是真的, $ pdf-> file_newname()//調用存儲在服務器中的輸出函數 $ pdf-> Output()//在屏幕上顯示什麼是保存在服務器中。 –

+0

然後她輸出重複次數。應該只在一個地方或另一個地方。 –

+0

我發現了麻煩,我錯過了$ filename:D –