2010-06-04 121 views
0

我wana打開PDF文件使用PHP,它運作良好,但它提示我下載該文件在指定的文件夾..我想要做的只是通過單擊一個鏈接的PDF文件打開沒有提示下載..任何人都有想法嗎?感謝名單提前..下面z上的代碼使用php打開pdf文件

PHP代碼:

$mypdf = PDF_new(); 
PDF_open_file($mypdf, ""); 
PDF_begin_page($mypdf, 595, 842); 
$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0); 
PDF_setfont($mypdf, $myfont, 10); 
PDF_show_xy($mypdf, "hello my first pdf converted file", 50, 750); 
PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730); 


PDF_end_page($mypdf); 
PDF_close($mypdf); 

$mybuf = PDF_get_buffer($mypdf); 
$mylen = strlen($mybuf); 
header("Content-type: application/pdf"); 
header("Content-Length: $mylen"); 
header("Content-Disposition: inline; filename=gen01.pdf"); 
print $mybuf; 

PDF_delete($mypdf); 

..................................... 
html code: 


<html> 
<body> 
Click here to see pdf file <a href="gen01.php" target="_blank">pdf1</a> 
</body> 
</html> 
+0

嗯,這將取決於客戶端瀏覽器(以及瀏覽器插件)。所以你不能確定它會顯示/打開。 – Macmade 2010-06-04 10:11:28

回答

1

刪除header("Content-Disposition: inline; filename=gen01.pdf"); 但如果訪問者不必與「應用程序/ PDF」 MIME類型相關的PDF閱讀器,該瀏覽器將下載該文件。

+0

捆綁ov thanx ..它的工作原理 – Sahar 2010-06-04 11:49:01

0

1.創建一個名爲fileHolder的mysql表,它具有以下屬性;

2.id,文件名

3.then使用HTML創建以下形式

<form enctype="multipart/form-data" action="upload.php" method="post"> 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
<input type="file" required name="uploaded_file"> 
<input type=submit value=upload > 
</form> 

4.然後使用以下代碼

if ((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { 
//Check if the file is JPEG image and it's size is less than 350Kb 
$filename = basename($_FILES['uploaded_file']['name']); 
$ext = substr($filename, strrpos($filename, '.') + 1); 
if (($ext == "pdf") && ($_FILES["uploaded_file"]["type"] == "application/pdf") && 
($_FILES["uploaded_file"]["size"] < 350000000)) { 
//Determine the path to which we want to save this file 
$newname = dirname(__FILE__) . '\\Abstract\\' . $filename; 
    //Check if the file with the same name is already exists on the server 
    if (!file_exists($newname)) { 
     //Attempt to move the uploaded file to it's new place 
     if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname))) { 
      echo "It's done! The file has been saved as: " . $newname; 
     } else { 
      echo "Error: A problem occurred during file upload!"; 
     } 
    } else { 
     echo "Error: File " . $_FILES["uploaded_file"]["name"] . " already exists"; 
    } 
} else { 
    echo "Error: Only .pdf images under 350MB are accepted for upload"; 
} 
} else { 
echo "Error: No file uploaded"; 
} 

創建upload.php的// ================================================== ====== 上面的代碼負責將文件放在一個名爲abstract的目錄中。

最後,我們需要在瀏覽器上查看該文件。使用以下

查詢在view.php文件

//假設你有連接,並選擇DB

header('Content-type: application/pdf'); 

select filename from fileHolder where id = 1; 

if (empty($filename) && !empty($_GET['filename'])) /*file name comes from a link called 
$filename = $_GET['filename'];      view which is used to identify the 
                exact file that we are lookingfor*/ 
$filenames = "..\\user\\Abstract\\".$filename.""; 
$file = fopen($filenames, "r"); 

enter code here $ FREAD = FREAD($文件,文件大小($文件名) ); echo $ fread;