実現したいこと
Webページでダウンロードボタンをクリックするとファイルがダウンロードされる仕様です。
画面は、すぐに別の画面(test2.view.php)に遷移したいです。
ダウンロードはされますが、遷移しません。
ご教示お願いたします。
ソースコード
PHP
1// index.php 2 3<?php 4 5include_once './view/test.view.php'
PHP
1// test.view.php 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <meta charset="utf-8"> 7 <title>test</title> 8</head> 9<body> 10 <form action="./download.php" method="POST"> 11 <input type="submit" value="submit"> 12 </form> 13</body> 14</html>
PHP
1// download.php 2 3<?php 4require_once './model/function.php'; 5 6$request_method = get_request_method(); 7 8if ($request_method === 'POST'){ 9 10 $result_file_name = '/tmp/test.zip'; 11 12 // ローカルダウンロード 13 download($result_file_name); 14 15 include_once './view/test2.view.php'; 16 17} 18 19exit; 20
PHP
1// test2.view.php 2 3<?php 4 5echo "<h1>Header Content</h1>"; 6echo "<p>"; 7echo " <span>This is a content.</span>"; 8echo "</p>"; 9 10?>
PHP
1// function.php 2 3function download($pPath, $pMimeType = null) 4{ 5 //ファイルが読めない時はエラー 6 if (!is_readable($pPath)) { die($pPath); } 7 8 //Content-Typeとして送信するMIMEタイプ(第2引数を渡さない場合は自動判定) 9 $mimeType = (isset($pMimeType)) ? $pMimeType 10 : (new finfo(FILEINFO_MIME_TYPE))->file($pPath); 11 12 //適切なMIMEタイプが得られない時は、未知のファイルを示すapplication/octet-streamとする 13 if (!preg_match('/\A\S+?\/\S+/', $mimeType)) { 14 $mimeType = 'application/octet-stream'; 15 } 16 17 //Content-Type 18 header('Content-Type: ' . $mimeType); 19 20 //ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する 21 header('X-Content-Type-Options: nosniff'); 22 23 //ダウンロードファイルのサイズ 24 header('Content-Length: ' . filesize($pPath)); 25 26 //ダウンロード時のファイル名 27 header('Content-Disposition: attachment; filename="' . basename($pPath) . '"'); 28 29 //keep-aliveを無効にする 30 header('Connection: close'); 31 32 //readfile()の前に出力バッファリングを無効化する 33 while (ob_get_level()) { ob_end_clean(); } 34 35 //出力 36 readfile($pPath); 37 38 exit; 39} 40 41
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。