牛骨文教育服务平台(让学习变的简单)
博文笔记

PHP文件下载函数

创建时间:2015-04-09 投稿人: 浏览次数:191
/**
	 * 下载本地文件
	 * @param  array    $file     文件信息数组
	 * @param  callable $callback 下载回调函数,一般用于增加下载次数
	 * @param  string   $args     回调函数参数
	 * @return boolean            下载失败返回false
	 */
	private function downLocalFile($file, $callback = null, $args = null){
		if(is_file($file["rootpath"].$file["savepath"].$file["savename"])){
			/* 调用回调函数新增下载数 */
			is_callable($callback) && call_user_func($callback, $args);

			/* 执行下载 */ //TODO: 大文件断点续传
			header("Content-Description: File Transfer");
			header("Content-type: " . $file["type"]);
			header("Content-Length:" . $file["size"]);
			if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) { //for IE
				header("Content-Disposition: attachment; filename="" . rawurlencode($file["name"]) . """);
			} else {
				header("Content-Disposition: attachment; filename="" . $file["name"] . """);
			}
			readfile($file["rootpath"].$file["savepath"].$file["savename"]);
			exit;
		} else {
			$this->error = "文件已被删除!";
			return false;
		}
	}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。