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;
}
}
}
}
?>