2010年4月2日 星期五

檔案轉向,防盜連

原文出處:
台灣PHP聯盟[ Taiwan PHP User Group ]

$dir = "files";
if($_GET['getfile']&&!preg_match('/\//i', $_GET['getfile'])){
$the_file = $dir . '/' . $_GET['getfile'];
if(file_exists($the_file)){
header('Content-disposition: attachment; filename='.$_GET['getfile']);
$fh = fopen($the_file, 'r');
while(!feof($fh)){
echo fgets($fh, 4096);
}
fclose($fh);
} else echo '您所選擇的檔案不存在!';
} else {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file!='.'&&$file!='..'&&!is_dir($dir.'/'.$file))
echo ''.$file.'
';
}
closedir($dh);
}
}
}

沒有留言:

張貼留言