File "download.php"
Full path: /www/co2/download.php
File size: 3.25 KiB (3333 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
//■ファイルダウンロードプログラム
//●文字コードの設定
// ブラウザ表示文字コード
$enc_disp = "SJIS";
// 保存ファイル名文字コード
$enc_file = "UTF-8";
//●ダウンロードするファイルの情報,フォルダの保存場所を取得
EXTRACT($_POST);
//●ダウンロードするフォルダの場所を設定
//$dir_name = "/www/upfile/simulation/file";
$dir_name = urldecode($_POST['s']);
//●ダウンロードするファイル名を設定
//$file_name = urldecode($_POST["f"]);
$file_name = date("Ymd" ,strtotime($_POST['date_'])) .".csv";
//●ダウンロードするファイルの場所を設定
$path = $dir_name . "/" .$file_name;
//●CSV出力処理
$item_name = mb_convert_encoding("端末番号,機械名,稼働時間" ."\n" ,$enc_disp,$enc_file);
$fp_csv = fopen($path,"w");
fwrite($fp_csv,$item_name);
//データ数カウント(行数)
$row_ct = count($_POST['out_terminal_id']);
for($i = 0;$i < $row_ct;$i++)
{
fwrite($fp_csv,mb_convert_encoding($_POST['out_terminal_id'][$i],$enc_disp,$enc_file) ."," .mb_convert_encoding($_POST['out_terminal_name'][$i],$enc_disp,$enc_file) ."," .$_POST['out_job'][$i] ."\n");
}
fclose($fp_csv);
//●ダウンロードしたファイル名をテキストファイルに保存(履歴として残す)
//$fpp = fopen("testlog.txt","a");
//fwrite($fpp,$file_name ."\n");
//fclose($fpp);
//●ダウンロードするファイルの拡張子を取得
$finfo = pathinfo($path);
$ext = $finfo["extension"];
//●拡張子からファイルの種類を設定
switch ($ext)
{
case "mpg":
case "mpeg":
header("Content-Type: video/mpeg");
break;
case "wmv":
header("Content-Type: video/x-ms-wmv");
break;
case "swf":
header("Content-Type: application/x-shockwave-flash");
break;
case "mp3":
header("Content-Type: audio/mpeg");
break;
case "mp4":
header("Content-Type: audio/mp4");
break;
case "wav":
header("Content-Type: audio/x-wav");
break;
case "mid":
case "midi":
header("Content-Type: audio/midi");
break;
case "mmf":
header("Content-Type: application/x-smaf");
break;
case "jfif":
case "jpe":
case "jpeg":
case "jpg":
header("Content-Type: image/jpeg");
break;
case "gif":
header("Content-Type: image/gif");
break;
case "png":
header("Content-Type: image/png");
break;
case "tif":
header("Content-Type: image/tif");
break;
case "pdf":
header("Content-Type: application/pdf");
break;
case "htm":
case "html":
header("Content-Type: text/html");
break;
case "xls":
header("Content-Type: application/vnd.ms-excel");
break;
case "doc":
header("Content-Type: application/msword");
break;
case "ppt":
header("Content-Type: application/vnd.ms-powerpoint");
break;
default:
header("Content-Type: application/octet-stream");
break;
}
//●ファイル名の文字コードを変換する
$det_enc = mb_detect_encoding($file_name, $enc_file . ", " . $enc_disp);
if ($det_enc and $det_enc != $enc_disp)
{
$file_name = mb_convert_encoding($file_name, $enc_disp, $det_enc);
}
//●ファイルダウンロード処理
header("Content-Disposition: attachment; filename=". $file_name);
$fp = fopen($path, "rb");
$data = fread($fp, filesize($path));
echo $data;
fclose($fp);
?>