ホームページ  >  記事  >  バックエンド開発  >  PHP必須ファイルのダウンロード

PHP必須ファイルのダウンロード

WBOY
WBOYオリジナル
2016-07-25 08:42:39890ブラウズ

新しいウィンドウを開かずに特定のファイルをダウンロードする必要がある場合は、次のコード スニペットが役に立ちます。

    function Force_download($file)
  1. {
  2. $dir = "../log/exports/";
  3. if ((isset($file))&&(file_exists($dir.$file))) {
  4. header("Content-type: application/force-download");
  5. header('Content-Disposition: inline; filename="' . $dir.$file . '"');
  6. header("Content-Transfer-エンコーディング: バイナリ");
  7. header("Content-length: ".filesize($dir.$file));
  8. header('Content-Type: application/octet-stream');
  9. header('Content-Disposition: Attachment; filename="' . $file . '"');
  10. readfile("$dir$file") } else {
  11. echo "ファイルが選択されていません" }
  12. }
使用法:

force_download("image.jpg");

?>
    コードをコピー
PHP

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。