/** * 上传头像 */ public function uploadavatar() { $auth_data = authcode($_GET['auth_data'], "DECODE"); if ($auth_data != AppframeAction::$Cache['uid']) { exit("0"); } if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) { //flashpost $avatardata = $GLOBALS['HTTP_RAW_POST_DATA']; } $dir = C("UPLOADFILEPATH") . "/avatar/" . AppframeAction::$Cache['uid'] . '/'; //创建图片存储文件夹 if (!file_exists($dir)) { mkdir($dir, 0777, true); } //存储flashpost图片压缩包 member/avatar/用户id/ $filename = $dir . AppframeAction::$Cache['uid'] . ".zip"; file_put_contents($filename, $avatardata); import("Pclzip"); $archive = new PclZip($filename); if ($archive->extract(PCLZIP_OPT_PATH, $dir) == 0) { die("Error : " . $archive->errorInfo(true)); } //判断文件安全,删除压缩包和非jpg图片 $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg'); if (defined("CONFIG_FTPSTATUS") && CONFIG_FTPSTATUS) { import("UploadFile"); import("Dir"); $Dir = new Dir(); $UploadFile = new UploadFile(); } if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file !== '.' && $file !== '..') { if (!in_array($file, $avatararr)) { @unlink($dir . $file); } else { $info = @getimagesize($dir . $file); if (!$info || $info[2] != 2) { @unlink($dir . $file); } } if (defined("CONFIG_FTPSTATUS") && CONFIG_FTPSTATUS) { //如果有开启FTP,则上传到FTP上 $UploadFile->FTPuplode($dir . $file, $dir . $file); } } } closedir($handle); } //如果是上传FTP,删除 if (defined("CONFIG_FTPSTATUS") && CONFIG_FTPSTATUS) { $Dir->delDir($dir); } //保存头像地址到数据库,默认保存90x90.jpg //20121214 废除,userpic用于保存非本地头像,例如QQ登陆后的第三方头像。 //M("Member")->where(array("userid" => AppframeAction::$Cache['uid']))->save(array("userpic" => "avatar/" . AppframeAction::$Cache['uid'] . '/90x90.jpg')); exit('1'); }
/** * 远程保存 * @param $value 传入下载内容 * @param $watermark 是否加入水印 * @param $ext 下载扩展名 */ public function download($value, $watermark = '', $ext = 'gif|jpg|jpeg|bmp|png') { //检查是否有开启CURL if (!function_exists('curl_init')) { return $value; } if ($watermark == '') { if ($this->Config['watermarkenable']) { $watermark = true; } } $curl = curl_init(); //正则匹配是否有符合数据 if (!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\\.({$ext}))\\2/i", $value, $matches)) { return $value; } $remotefileurls = array(); //过滤域名 $NoDomain = explode("|", CONFIG_FILEEXCLUDE); //当前程序所在域名地址 $NoDomain[] = urlDomain(get_url()); //附件地址 $upload_url = urlDomain(CONFIG_SITEFILEURL); foreach ($matches[3] as $matche) { //过滤远程地址 if (strpos($matche, '://') === false) { continue; } //过滤后台设置的域名,和本站域名 if (in_array(urlDomain($matche), $NoDomain)) { continue; } $remotefileurls[] = $matche; } //保存目录 $FilePath = $this->FilePath(); $oldpath = $newpath = array(); import('UploadFile'); $upload = new UploadFile(); foreach ($remotefileurls as $k => $file) { if (strpos($file, '://') === false || strpos($file, $upload_url) !== false) { continue; } //取得文件扩展 $file_fileext = fileext($file); //取得文件名 $file_name = basename($file); //保存文件名 $filename = uniqid() . "." . $file_fileext; // 设置你需要抓取的URL curl_setopt($curl, CURLOPT_URL, $file); // 设置header curl_setopt($curl, CURLOPT_HEADER, 0); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //超时设置 curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 运行cURL,请求网页 $filedata = curl_exec($curl); //保存图片 $status = $this->build_file($filedata, $FilePath . "/" . $filename); if ($status) { //加水印 if ($watermark) { $this->water($FilePath . "/" . $filename, "", $FilePath . "/" . $filename); } $oldpath[] = $file; $newpath[] = str_replace($this->path, CONFIG_SITEFILEURL, $FilePath . "/" . $filename); $info = array("name" => $file_name, "type" => "", "size" => filesize($FilePath . "/" . $filename), "key" => "", "extension" => $file_fileext, "savepath" => $FilePath, "savename" => "/" . $filename, "hash" => md5(str_replace($this->path, "", $FilePath . "/" . $filename))); $aid = $this->FileData($info); //设置标识 $this->upload_json($aid, $this->filehttp, $filename); //FTP if ($this->Ftpstatus) { // 远程存放地址 $remote = $this->Config['ftpuppat'] . str_replace(SITE_PATH . "/", "", $FilePath . "/" . $filename); if ($upload->FTPuplode($FilePath . "/" . $filename, $remote)) { //删除 unlink($FilePath . "/" . $filename); } } } } // 关闭URL请求 curl_close($curl); $value = str_replace($oldpath, $newpath, $value); return $value; }