注意:网站使用CDN上的图片有利有减轻网站的压力,同时也要注意在本地存一份同路径的图片,当云盘有问题时,可以马上切换到本地来访问。
/* 在原有的编辑器基础上增加图片上传到cdn的功能*/
header("Content-Type:text/html;charset=utf-8");
error_reporting(E_ERROR|E_WARNING);
$Path = "./images/";
/* 设置文件的存储路径 */
$ymd = date("Y") . date("m") . date("d"); // 文件路径格式
$file_save_url = $Path . $ymd . "/"; // 本地存储路径
$save_url = $ymd . "/"; // 优盘云存储路径
include "upyun.class.php"; // 引入优盘云处理类
include "upyun_config.php"; // 优盘云配置
// 实例化优盘云
$upyun = new UpYun($img_bucketname, $img_username, $img_password);
try {
$file=$file_save_url.$info['name']; // 本地路径加文件名
$con = $save_url.$info['name']; // 优盘云路径加文件名
$opts = array(
UpYun::CONTENT_MD5 => md5(file_get_contents($file))
);
$fh = fopen($file, "rb");
// 上传图片,自动创建目录
$rsp = $upyun->writeFile("/".$con, $fh, True, $opts);
fclose($fh);
} catch (Exception $e) {
$filename = "error_log.txt";
$errorcontent = "image " . date("Y-m-d H:m:s") . " " . $e->getCode()." " . $e->getMessage() . "\r\n";
$handle = fopen($filename, 'a');
fwrite($handle, $errorcontent);
fclose($handle);
exit();
}