<?php require_once '../libs/BaiduPCS.class.php'; //请根据实际情况更新$access_token与$appName参数 $access_token = '3.839af46f54db6ed60797847d2febbca0.2592000.1359262544.754976761-248414'; //应用目录名 $appName = '测试应用'; //应用根目录 $root_dir = '/apps' . '/' . $appName . '/'; //路径 $path = $root_dir . 'yun.jpg'; $pcs = new BaiduPCS($access_token); $result = $pcs->downloadStream($path); header('Content-Disposition:attachment;filename="yun.jpg"'); header('Content-Type:application/octet-stream'); echo $result;
// 利用上面拿到的path构建远程目录中的文件 $path = str_replace('//', '/', REMOTE_ROOT . '/' . $path); $file_ext = strtolower(substr($path, strrpos($path, '.') + 1)); $file_name = substr($path, strrpos($path, '/') + 1); // 缓存啦 header("Cache-Control: private, max-age=10800, pre-check=10800"); header("Pragma: private"); header("Expires: " . date(DATE_RFC822, strtotime(" 10 day"))); if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304); exit; } // 开始打印远程文件的内容 $BaiduPCS = new BaiduPCS(BAIDU_TOKEN); if (in_array($file_ext, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) { $result = $BaiduPCS->downloadStream($path); $meta = json_decode($result, true); if (isset($meta['error_msg'])) { header("Content-Type: text/html; charset=utf8"); echo $meta['error_msg']; exit; } header('Content-type: image/jpeg'); } elseif (in_array($file_ext, array('mp3', 'ogg', 'wma', 'wav', 'mp3pro', 'mid', 'midi'))) { $result = $BaiduPCS->downloadStream($path); $meta = json_decode($result, true); if (isset($meta['error_msg'])) { header("Content-Type: text/html; charset=utf8"); echo $meta['error_msg']; exit; }