「PHP」サーバーのファイルをダウンロードするサンプルコード

phpコード
public function servDownloadFileAction()
{
define('CFT_WEB_ROOT’,’http://’.$_SERVER[“SERVER_NAME"]);
$file_name = “template.xlsx";
$file_dir = CFT_WEB_ROOT."/www/public/template/"; –
$file = @ fopen($file_dir . $file_name,"r");
if (!$file) {
echo “ファイルが存在しません.";
} else {
Header(“Content-type: application/octet-stream");
Header(“Content-Disposition: attachment; filename=" . $file_name);
while (!feof ($file)) {
echo fread($file,50000);
}
fclose ($file);
}
}

PHP

Posted by arkgame