/** * 根据指定的路径创建不存在的文件夹 * * @param string $path 路径/文件夹名称 * * @return string */ private function makePath($path) { $path = dirname($path); if (!is_dir($path) && !mkdir($path, 0700, true)) { \Cml\throwException(Lang::get('_CREATE_DIR_ERROR_') . "[{$path}]"); } return true; }
/** * 下载文件 * 可以指定下载显示的文件名,并自动发送相应的Header信息 * 如果指定了content参数,则下载该参数的内容 * * @param string $filename 下载文件名 * @param string $showname 下载显示的文件名 * @param string $content 下载的内容 * @param integer $expire 下载内容浏览器缓存时间 * * @return void */ public static function download($filename, $showname = '', $content = '', $expire = 180) { $length = 0; $uploadPath = CML_PROJECT_PATH . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'Uploads' . DIRECTORY_SEPARATOR; if (is_file($filename)) { $length = filesize($filename); } elseif (is_file($uploadPath . $filename)) { $filename = $uploadPath . $filename; $length = filesize($filename); } elseif ($content != '') { $length = strlen($content); } else { \Cml\throwException($filename . '下载文件不存在!'); } if (empty($showname)) { $showname = $filename; } $showname = basename($showname); if (!empty($filename)) { $type = self::mimeContentType($filename); } else { $type = "application/octet-stream"; } //发送Http Header信息 开始下载 header("Pragma: public"); header("Cache-control: max-age=" . $expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s", Cml::$nowTime + $expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s", Cml::$nowTime) . "GMT"); header("Content-Disposition: attachment; filename=" . $showname); header("Content-Length: " . $length); header("Content-type: " . $type); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary"); if ($content == '') { readfile($filename); } else { echo $content; } exit; }