2013-02-05 38 views
2

我是一個新手。嘗試圍繞PHP和JavaScript發展我的知識。這一次,我多次抨擊我的頭,足夠長的時間尋找答案。如何通過代理頁保存正確的文件名

我試圖創建一個代理頁面來查看PDF文件。有用!但是當我想保存文件時,它將自己命名爲「proxy.pdf」。在我的Transformer Prime平板電腦上,它下載腳本:「proxy.php」。

這是我代理的PHP代碼的樣子:

<?php 

session_start; 

// define path to image folder 
$image_folder = realpath(dirname(__FILE__))."/imagefolder/"; 
// get image name from the query string 
// no probe 
if (isset($_GET['pic']) && basename($_GET['pic']) == $_GET['pic']) 
{ 
    $pic = $image_folder.$_GET['pic']; 
    if (file_exists($pic) && is_readable($pic)) 
    { 
     // get the filename extension 
     $ext = substr($pic, -3); 
     // set the MIME type 
     switch ($ext) 
     { 
    case 'jpg': 
    $mime = 'image/jpeg'; 
    break; 
    case 'gif': 
    $mime = 'image/gif'; 
    break; 
    case 'png': 
    $mime = 'image/png'; 
    break; 
    case 'pdf': 
    //alt octet-stream 
    $mime = 'application/pdf'; 
    break; 
    default: 
    $mime = false; 
    } 
    // if a valid MIME type exists, display the image 
    // by sending appropriate headers and streaming the file 
    if ($mime) 
    { 

     header('Content-type: '.$mime); 
     header('Content-length: '.filesize($pic)); 

     $file = fopen($pic, 'rb'); 
     if ($file) 
     { 
      fpassthru($file); 
      exit; 
     } 
    } 
} 
} 
?> 

回答

0

我碰到這個偶然,當我有我的ASP.NET應用程序相同的問題。使用Content-Disposition標題對我來說是什麼。我不會假裝成爲PHP專家,但如果您將此行添加到其他標題旁邊,它應該可以完成這項工作。

header('Content-Disposition: attachment; filename='.$_GET['pic']); 
相關問題