ファイルをダウンロードするためのこの小さな PHP スクリプトを作成しました。ファイルは正常に動作してい"zip"
ますが、ファイルをダウンロードできません"rar"
。
スクリプトは次のとおりです。
$file_path = $_SERVER['DOCUMENT_ROOT'].'/'.'ps-friend-redesigned'.'/' .$RESULT_ARRAY['bd_brushfilepath'];
function download_file($file, $name, $mime_type='')
{
if(!is_readable($file)) die('File not found.');
$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
"php" => "text/plain",
"rar" => "application/x-rar-compressed"
);
if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else{
$mime_type="application/force-download";
};
};
@ob_end_clean();
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; file="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
readfile($file);
}
今、ファイルをダウンロードしようとすると、2 つの四角形が続く"rar"
コンテンツを含む PHP ファイルがダウンロードされます。Rar!
そして、この PHP ファイルのサイズは、実際の RAR ファイルと同じです。"rar" => "application/x-rar-compressed"
この配列から除外しようとしまし$known_mime_types
たが、まだ機能しません。私は問題をまったく理解できないようです。
アップデート:
私はこのコードを試してMIMEタイプを取得しました(ここの回答の1つからコピーされたコード)。
function get_mime($file) {
if (function_exists("finfo_file")) {
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$mime = finfo_file($finfo, $file);
finfo_close($finfo);
return $mime;
} else if (function_exists("mime_content_type")) {
return mime_content_type($file);
} else if (!stristr(ini_get("disable_functions"), "shell_exec")) {
// http://stackoverflow.com/a/134930/1593459
$file = escapeshellarg($file);
$mime = shell_exec("file -bi " . $file);
return $mime;
} else {
return false;
}
}
この関数の結果をエコーしても何も得られません。