download this file: header_ftp.php view text/plain: header_ftp.php file encoding: UTF-8 [goback]
<?php
##
function file_get_encding($filename)
{
static $encodings = array('UTF-8', 'EUC-KR', 'ASCII');
$contents = file_get_contents($filename);
return mb_detect_encoding($contents, $encodings);
}
//$ruri= $_SERVER['REQUEST_URI'];
$rfile = $_SERVER['SCRIPT_NAME'];
$rqstr = $_SERVER['QUERY_STRING'] ?? '';
$rpath = $_SERVER['PATH_INFO'] ?? '';
$file = $_SERVER['DOCUMENT_ROOT'] . $rfile;
if(!preg_match(';^/ftp/.+\.php$;i',$rfile) OR !file_exists($file))
{
return;
}
$filename = basename($rfile);
$filesize = filesize($file);
if(preg_match(';download;i', $rqstr.$rpath))
{
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Type: application/force-download');
header("Content-Length: $filesize");
readfile($file);
exit;
}
$fileenc = file_get_encding($file);
$charset = $fileenc ? "; charset=$fileenc" : '';
if(!$rqstr && !$rpath)
{
header('Content-Type: text/html'.$charset);
echo "download this file: <a href=$rfile/download>$filename</a> \n";
echo "view text/plain: <a href=$rfile/text>$filename</a> file encoding: $fileenc \n";
echo "<a href='".dirname($rfile)."/'>[goback]</a>\n";
echo "<hr>\n";
highlight_file($file); // view source
exit;
}
// print plain text
header('Content-Type: text/plain'.$charset);
header("Content-Length: $filesize");
readfile($file);
exit; // do not any print
?>