
ファイルをアップロードして、それを一覧表示してファイルを選択するとダウンロードするという処理を書きたくて、いろいろネットで調べて書きました。
一覧表示までは出来たのですが、どうもその先のダウンロードが出来ません。
以下は、一覧表示してダウンロードをするコードです。
PHP
1<?php 2 3function download_file($path_file) 4{ 5 /* ファイルの存在確認 */ 6 if (!file_exists($path_file)) { 7 die("Error: File(".$path_file.") does not exist"); 8 } 9 10 /* オープンできるか確認 */ 11 if (!($fp = fopen($path_file, "r"))) { 12 die("Error: Cannot open the file(".$path_file.")"); 13 } 14 fclose($fp); 15 16 /* ファイルサイズの確認 */ 17 if (($content_length = filesize($path_file)) == 0) { 18 die("Error: File size is 0.(".$path_file.")"); 19 } 20 21 /* ダウンロード用のHTTPヘッダ送信 */ 22 header("Content-Disposition: inline; filename=\"".basename($path_file)."\""); 23 header("Content-Length: ".$content_length); 24 header("Content-Type: application/octet-stream"); 25 26 /* ファイルを読んで出力 */ 27 if (!readfile($path_file)) { 28 die("Cannot read the file(".$path_file.")"); 29 } 30} 31 32$filepath = "C:/upload/failename.txt"; 33 34$fp = fopen($filepath, "r"); 35 36$array = file($filepath); 37foreach ($array as $line) { 38 list($title, $file) = explode("\t", trim($line)); 39 $file = 'C:/upload/' . $file; 40 if (file_exists($file)) { 41 print '<a href="'.$file.'" onClick="download_file($file)">' . $title . '</a><br />'; 42 var_dump($file).PHP_EOL; 43 } 44} 45fclose($fp); 46?> 47
僕の考えでは、onClickでファイルパス$fileを引数として関数download_fileに渡しているのですが、これでは出来ないのでしょうか?
回答3件
あなたの回答
tips
プレビュー