您的当前位置:首页正文

禁止直接访问文件后,php读取指定位置文件示例

2024-11-10 来源:个人技术集锦
<?php 
if (isset($_GET['filename'])) {
	$filepath = 'upload/' . $_GET['filename'];

	if (file_exists($filepath)) {
		header('Content-Description: File Transfer');
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment; filename="' . $_GET['filename'] . '"');
		header('Content-Transfer-Encoding: binary');
		header('Expires: 0');
		header('Cache-Control: must-revalidate');
		header('Pragma: public');
		header('Content-Length: ' . filesize($filepath));
		ob_clean();
		flush();
		readfile($filepath);
		exit;
	} else {
		echo "文件不存在。";
	}
}
?>


显示全文