添付ファイルを一覧表示する Web サイトがあります。これらの添付ファイルをクリックすると、次の 2 種類の動作のいずれかが発生します。
- 添付ファイルが同じウィンドウで開きます。
- 添付ファイルは、ドキュメントを開くか保存するためのダイアログをユーザーに提示します。
番号 1 は PDF でのみ発生しているように見えますが、すべての添付ファイルに保存/開く/キャンセルのポップアップをユーザーに表示させる方法はありますか?
PDFのみ..
// The user will receive a PDF to download
header('Content-type: application/pdf');
// File will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The actual PDF file on the server is source.pdf
readfile('source.pdf');
...
echo mime_content_type('Yourfile.ext')
他のすべてのファイルタイプには、おそらく .. oを使用できます
header('Content-type: '.mime_content_type('Yourfile.ext'));
header('Content-Disposition: attachment; filename="'.$output_filename.'"');
readfile($source_filename);
私はそれをテストしていないことに注意してください...
Content-type ヘッダーは、MIME タイプで指定された、ダウンロードするファイルのタイプを指定します。Content-Disposition ヘッダーは、ダウンロードするファイルの新しいファイル名を指定します。readfile 行は、送信されるヘッダーではなく、ファイルからすべてのデータを取得して出力する PHP 呼び出しです。関数 readfile に渡す引数は、ダウンロードする実際の pdf ファイルの場所です。
アップデート
mime_content_type()
関数は非推奨です。あなたはこれと交換する必要があります...
$finfo = new finfo;
$fileinfo = $finfo->file($file, FILEINFO_MIME);
適切な content-type headers() を送信する必要があります。
例 (pdf の場合ですが、MIME タイプを調整すれば何にでも機能します):
<?php
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf'); // make sure this is the correct mime-type for the file
readfile('huge_document.pdf');
?>
追加資料:
私はこれをユニバーサル MIME として使用してきました。必要なのは、いくつかのパラメータを渡すことだけです。
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
$size=get_size($url);
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Content-Length: '.$size);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
}
else
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Content-Length: '.$size);
header('Pragma: no-cache');
}
readfile($url);
exit;
}
// Not found
exit('File not found 8{');
次のようにリンクを渡します。
<a href="download.php?mime=++your file ext++&title=++ your file title ++&token=++your link to a file++">Download my file</a>
タイトルはオプションです:)
@HappyApe から親切に提供された回答を使用して、Wordpress で添付ファイルのリンクをクリックしたときにドキュメントを強制的にダウンロードするために使用する完全なコードを次に示します。
/**
* Check that a page is the permalink of an attachment and force the download of the attahment if it is
*/
add_action('template_redirect', 'template_redirect');
function template_redirect(){
global $post;
/** Get the attachment URL as a pertty link ($url) and as a link to the file ($guid) */
$url = get_permalink($post->ID);
$guid = wp_get_attachment_url($post->ID);
/** Get the file name of the attachment to output in the header */
$file_name = basename(get_attached_file($post->ID));
/** Get the location of the file on the server */
$file_location = $_SERVER['DOCUMENT_ROOT'].substr($guid, strpos($guid, '/wp-content'));
/** Get the file Mime Type */
$finfo = new finfo;
$mime_type = $finfo->file($file_location, FILEINFO_MIME);
/** Check that the page we are on is a local attachment and force download if it is */
if(is_local_attachment($url)) :
header("Content-type: ".$mime_type, true, 200);
header("Content-Disposition: attachment; filename=".$file_name);
header("Pragma: no-cache");
header("Expires: 0");
readfile($guid);
exit();
endif;
}