file.func.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function file_write($filename, $data) {
  8. global $_W;
  9. $filename = ATTACHMENT_ROOT . '/' . $filename;
  10. mkdirs(dirname($filename));
  11. file_put_contents($filename, $data);
  12. @chmod($filename, $_W['config']['setting']['filemode']);
  13. return is_file($filename);
  14. }
  15. function file_read($filename) {
  16. global $_W;
  17. $filename = ATTACHMENT_ROOT . '/' . $filename;
  18. if (!is_file($filename)) {
  19. return false;
  20. }
  21. return file_get_contents($filename);
  22. }
  23. function file_move($filename, $dest) {
  24. global $_W;
  25. mkdirs(dirname($dest));
  26. if (is_uploaded_file($filename)) {
  27. move_uploaded_file($filename, $dest);
  28. } else {
  29. rename($filename, $dest);
  30. }
  31. @chmod($filename, $_W['config']['setting']['filemode']);
  32. return is_file($dest);
  33. }
  34. function file_tree($path, $include = array()) {
  35. $files = array();
  36. if (!empty($include)) {
  37. $ds = glob($path . '/{' . implode(',', $include) . '}', GLOB_BRACE);
  38. } else {
  39. $ds = glob($path . '/*');
  40. }
  41. if (is_array($ds)) {
  42. foreach ($ds as $entry) {
  43. if (is_file($entry)) {
  44. $files[] = $entry;
  45. }
  46. if (is_dir($entry)) {
  47. $rs = file_tree($entry);
  48. foreach ($rs as $f) {
  49. $files[] = $f;
  50. }
  51. }
  52. }
  53. }
  54. return $files;
  55. }
  56. function mkdirs($path) {
  57. if (!is_dir($path)) {
  58. mkdirs(dirname($path));
  59. mkdir($path);
  60. }
  61. return is_dir($path);
  62. }
  63. function file_copy($src, $des, $filter) {
  64. $dir = opendir($src);
  65. @mkdir($des);
  66. while (false !== ($file = readdir($dir))) {
  67. if (($file != '.') && ($file != '..')) {
  68. if (is_dir($src . '/' . $file)) {
  69. file_copy($src . '/' . $file, $des . '/' . $file, $filter);
  70. } elseif (!in_array(substr($file, strrpos($file, '.') + 1), $filter)) {
  71. copy($src . '/' . $file, $des . '/' . $file);
  72. }
  73. }
  74. }
  75. closedir($dir);
  76. }
  77. function rmdirs($path, $clean = false) {
  78. if (!is_dir($path)) {
  79. return false;
  80. }
  81. $files = glob($path . '/*');
  82. if ($files) {
  83. foreach ($files as $file) {
  84. is_dir($file) ? rmdirs($file) : @unlink($file);
  85. }
  86. }
  87. return $clean ? true : @rmdir($path);
  88. }
  89. function file_upload($file, $type = 'image', $name = '', $compress = false) {
  90. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  91. if (empty($file)) {
  92. return error(-1, '没有上传内容');
  93. }
  94. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  95. return error(-2, '未知的上传类型');
  96. }
  97. global $_W;
  98. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  99. $ext = strtolower($ext);
  100. $setting = setting_load('upload');
  101. switch ($type) {
  102. case 'image':
  103. case 'thumb':
  104. $allowExt = array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'ico');
  105. $limit = $setting['upload']['image']['limit'];
  106. break;
  107. case 'voice':
  108. case 'audio':
  109. $allowExt = array('mp3', 'wma', 'wav', 'amr');
  110. $limit = $setting['upload']['audio']['limit'];
  111. break;
  112. case 'video':
  113. $allowExt = array('rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4');
  114. $limit = $setting['upload']['audio']['limit'];
  115. break;
  116. }
  117. $setting = $_W['setting']['upload'][$type];
  118. if (!empty($setting)) {
  119. $allowExt = array_merge($setting['extentions'], $allowExt);
  120. }
  121. if (!in_array(strtolower($ext), $allowExt) || in_array(strtolower($ext), $harmtype)) {
  122. return error(-3, '不允许上传此类文件');
  123. }
  124. if (!empty($limit) && $limit * 1024 < filesize($file['tmp_name'])) {
  125. return error(-4, "上传的文件超过大小限制,请上传小于 {$limit}k 的文件");
  126. }
  127. $result = array();
  128. if (empty($name) || $name == 'auto') {
  129. $uniacid = intval($_W['uniacid']);
  130. $path = "{$type}s/{$uniacid}/" . date('Y/m/');
  131. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  132. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  133. $result['path'] = $path . $filename;
  134. } else {
  135. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $name));
  136. if (!strexists($name, $ext)) {
  137. $name .= '.' . $ext;
  138. }
  139. $result['path'] = $name;
  140. }
  141. $save_path = ATTACHMENT_ROOT . '/' . $result['path'];
  142. if (!file_move($file['tmp_name'], $save_path)) {
  143. return error(-1, '保存上传文件失败');
  144. }
  145. if ($type == 'image' && $compress) {
  146. file_image_quality($save_path, $save_path, $ext);
  147. }
  148. $result['success'] = true;
  149. return $result;
  150. }
  151. function file_wechat_upload($file, $type = 'image', $name = '') {
  152. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  153. if (empty($file)) {
  154. return error(-1, '没有上传内容');
  155. }
  156. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  157. return error(-2, '未知的上传类型');
  158. }
  159. global $_W;
  160. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  161. $ext = strtolower($ext);
  162. if (in_array(strtolower($ext), $harmtype)) {
  163. return error(-3, '不允许上传此类文件');
  164. }
  165. $result = array();
  166. if (empty($name) || $name == 'auto') {
  167. $uniacid = intval($_W['uniacid']);
  168. $path = "{$type}s/{$uniacid}/" . date('Y/m/');
  169. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  170. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  171. $result['path'] = $path . $filename;
  172. } else {
  173. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $name));
  174. if (!strexists($name, $ext)) {
  175. $name .= '.' . $ext;
  176. }
  177. $result['path'] = $name;
  178. }
  179. $save_path = ATTACHMENT_ROOT . '/' . $result['path'];
  180. if (!file_move($file['tmp_name'], $save_path)) {
  181. return error(-1, '保存上传文件失败');
  182. }
  183. if ($type == 'image') {
  184. file_image_quality($save_path, $save_path, $ext);
  185. }
  186. $result['success'] = true;
  187. return $result;
  188. }
  189. function file_remote_upload($filename, $auto_delete_local = true) {
  190. global $_W;
  191. if (empty($_W['setting']['remote']['type'])) {
  192. return false;
  193. }
  194. if ($_W['setting']['remote']['type'] == '1') {
  195. load()->library('ftp');
  196. $ftp_config = array(
  197. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  198. 'username' => $_W['setting']['remote']['ftp']['username'],
  199. 'password' => $_W['setting']['remote']['ftp']['password'],
  200. 'port' => $_W['setting']['remote']['ftp']['port'],
  201. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  202. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  203. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  204. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  205. );
  206. $ftp = new Ftp($ftp_config);
  207. if (true === $ftp->connect()) {
  208. $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename);
  209. if ($auto_delete_local) {
  210. file_delete($filename);
  211. }
  212. if (!empty($response)) {
  213. return true;
  214. } else {
  215. return error(1, '远程附件上传失败,请检查配置并重新上传');
  216. }
  217. } else {
  218. return error(1, '远程附件上传失败,请检查配置并重新上传');
  219. }
  220. } elseif ($_W['setting']['remote']['type'] == '2') {
  221. load()->library('oss');
  222. load()->model('attachment');
  223. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  224. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . '.aliyuncs.com';
  225. try {
  226. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  227. $ossClient->uploadFile($_W['setting']['remote']['alioss']['bucket'], $filename, ATTACHMENT_ROOT . $filename);
  228. } catch (\OSS\Core\OssException $e) {
  229. return error(1, $e->getMessage());
  230. }
  231. if ($auto_delete_local) {
  232. file_delete($filename);
  233. }
  234. } elseif ($_W['setting']['remote']['type'] == '3') {
  235. load()->library('qiniu');
  236. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  237. $config = new Qiniu\Config();
  238. $uploadmgr = new Qiniu\Storage\UploadManager($config);
  239. $putpolicy = Qiniu\base64_urlSafeEncode(json_encode(array(
  240. 'scope' => $_W['setting']['remote']['qiniu']['bucket'] . ':' . $filename,
  241. )));
  242. $uploadtoken = $auth->uploadToken($_W['setting']['remote']['qiniu']['bucket'], $filename, 3600, $putpolicy);
  243. list($ret, $err) = $uploadmgr->putFile($uploadtoken, $filename, ATTACHMENT_ROOT . '/' . $filename);
  244. if ($auto_delete_local) {
  245. file_delete($filename);
  246. }
  247. if ($err !== null) {
  248. return error(1, '远程附件上传失败,请检查配置并重新上传');
  249. } else {
  250. return true;
  251. }
  252. } elseif ($_W['setting']['remote']['type'] == '4') {
  253. if (!empty($_W['setting']['remote']['cos']['local'])) {
  254. load()->library('cos');
  255. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  256. $uploadRet = qcloudcos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  257. } else {
  258. load()->library('cosv3');
  259. $uploadRet = \Qcloud_cos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  260. }
  261. if ($uploadRet['code'] != 0) {
  262. switch ($uploadRet['code']) {
  263. case -62:
  264. $message = '输入的appid有误';
  265. break;
  266. case -79:
  267. $message = '输入的SecretID有误';
  268. break;
  269. case -97:
  270. $message = '输入的SecretKEY有误';
  271. break;
  272. case -166:
  273. $message = '输入的bucket有误';
  274. break;
  275. }
  276. return error(-1, $message);
  277. }
  278. if ($auto_delete_local) {
  279. file_delete($filename);
  280. }
  281. }
  282. }
  283. function file_dir_remote_upload($dir_path) {
  284. global $_W;
  285. if (empty($_W['setting']['remote']['type'])) {
  286. return error(1, '未开启远程附件');
  287. }
  288. $dir_path = safe_gpc_path($dir_path);
  289. if (!empty($dir_path)) {
  290. $local_attachment = file_tree($dir_path);
  291. } else {
  292. $local_attachment = array();
  293. }
  294. if (is_array($local_attachment) && !empty($local_attachment)) {
  295. foreach ($local_attachment as $attachment) {
  296. $filename = str_replace(ATTACHMENT_ROOT, '', $attachment);
  297. list($image_dir, $file_account) = explode('/', $filename);
  298. if ($file_account == 'global' || !file_is_image($attachment)) {
  299. continue;
  300. }
  301. if (is_numeric($file_account) && is_dir(ATTACHMENT_ROOT . 'images/' . $file_account) && !empty($_W['setting']['remote_complete_info'][$file_account]['type'])) {
  302. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'][$file_account];
  303. } else {
  304. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'];
  305. }
  306. $result = file_remote_upload($filename);
  307. if (is_error($result)) {
  308. return $result;
  309. }
  310. }
  311. }
  312. return true;
  313. }
  314. function file_random_name($dir, $ext) {
  315. do {
  316. $filename = random(30) . '.' . $ext;
  317. } while (file_exists($dir . $filename));
  318. return $filename;
  319. }
  320. function file_delete($file) {
  321. if (empty($file)) {
  322. return false;
  323. }
  324. if (file_exists($file)) {
  325. @unlink($file);
  326. }
  327. if (file_exists(ATTACHMENT_ROOT . '/' . $file)) {
  328. @unlink(ATTACHMENT_ROOT . '/' . $file);
  329. }
  330. return true;
  331. }
  332. function file_remote_delete($file) {
  333. global $_W;
  334. if (empty($file)) {
  335. return true;
  336. }
  337. if ($_W['setting']['remote']['type'] == '1') {
  338. load()->library('ftp');
  339. $ftp_config = array(
  340. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  341. 'username' => $_W['setting']['remote']['ftp']['username'],
  342. 'password' => $_W['setting']['remote']['ftp']['password'],
  343. 'port' => $_W['setting']['remote']['ftp']['port'],
  344. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  345. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  346. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  347. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  348. );
  349. $ftp = new Ftp($ftp_config);
  350. if (true === $ftp->connect()) {
  351. if ($ftp->delete_file($file)) {
  352. return true;
  353. } else {
  354. return error(1, '删除附件失败,请检查配置并重新删除');
  355. }
  356. } else {
  357. return error(1, '删除附件失败,请检查配置并重新删除');
  358. }
  359. } elseif ($_W['setting']['remote']['type'] == '2') {
  360. load()->model('attachment');
  361. load()->library('oss');
  362. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  363. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . '.aliyuncs.com';
  364. try {
  365. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  366. $ossClient->deleteObject($_W['setting']['remote']['alioss']['bucket'], $file);
  367. } catch (\OSS\Core\OssException $e) {
  368. return error(1, '删除oss远程文件失败');
  369. }
  370. } elseif ($_W['setting']['remote']['type'] == '3') {
  371. load()->library('qiniu');
  372. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  373. $bucketMgr = new Qiniu\Storage\BucketManager($auth);
  374. $error = $bucketMgr->delete($_W['setting']['remote']['qiniu']['bucket'], $file);
  375. if ($error instanceof Qiniu\Http\Error) {
  376. if ($error->code() == 612) {
  377. return true;
  378. }
  379. return error(1, '删除七牛远程文件失败');
  380. } else {
  381. return true;
  382. }
  383. } elseif ($_W['setting']['remote']['type'] == '4') {
  384. $bucketName = $_W['setting']['remote']['cos']['bucket'];
  385. $path = '/' . $file;
  386. if (!empty($_W['setting']['remote']['cos']['local'])) {
  387. load()->library('cos');
  388. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  389. $result = qcloudcos\Cosapi::delFile($bucketName, $path);
  390. } else {
  391. load()->library('cosv3');
  392. $result = Qcloud_cos\Cosapi::delFile($bucketName, $path);
  393. }
  394. if (!empty($result['code'])) {
  395. return error(-1, '删除cos远程文件失败');
  396. } else {
  397. return true;
  398. }
  399. }
  400. return true;
  401. }
  402. function file_image_thumb($srcfile, $desfile = '', $width = 0) {
  403. global $_W;
  404. load()->classs('image');
  405. if (intval($width) == 0) {
  406. load()->model('setting');
  407. $width = intval($_W['setting']['upload']['image']['width']);
  408. }
  409. if (empty($desfile)) {
  410. $ext = pathinfo($srcfile, PATHINFO_EXTENSION);
  411. $srcdir = dirname($srcfile);
  412. do {
  413. $desfile = $srcdir . '/' . random(30) . ".{$ext}";
  414. } while (file_exists($desfile));
  415. }
  416. $des = dirname($desfile);
  417. if (!file_exists($des)) {
  418. if (!mkdirs($des)) {
  419. return error('-1', '创建目录失败');
  420. }
  421. } elseif (!is_writable($des)) {
  422. return error('-1', '目录无法写入');
  423. }
  424. $org_info = @getimagesize($srcfile);
  425. if ($org_info) {
  426. if ($width == 0 || $width > $org_info[0]) {
  427. copy($srcfile, $desfile);
  428. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  429. }
  430. }
  431. $scale_org = $org_info[0] / $org_info[1];
  432. $height = $width / $scale_org;
  433. $desfile = Image::create($srcfile)->resize($width, $height)->saveTo($desfile);
  434. if (!$desfile) {
  435. return false;
  436. }
  437. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  438. }
  439. function file_image_crop($src, $desfile, $width = 400, $height = 300, $position = 1) {
  440. load()->classs('image');
  441. $des = dirname($desfile);
  442. if (!file_exists($des)) {
  443. if (!mkdirs($des)) {
  444. return error('-1', '创建目录失败');
  445. }
  446. } elseif (!is_writable($des)) {
  447. return error('-1', '目录无法写入');
  448. }
  449. return Image::create($src)
  450. ->crop($width, $height, $position)
  451. ->saveTo($desfile);
  452. }
  453. function file_lists($filepath, $subdir = 1, $ex = '', $isdir = 0, $md5 = 0, $enforcement = 0) {
  454. static $file_list = array();
  455. if ($enforcement) {
  456. $file_list = array();
  457. }
  458. $flags = $isdir ? GLOB_ONLYDIR : 0;
  459. $list = glob($filepath . '*' . (!empty($ex) && empty($subdir) ? '.' . $ex : ''), $flags);
  460. if (!empty($ex)) {
  461. $ex_num = strlen($ex);
  462. }
  463. foreach ($list as $k => $v) {
  464. $v = str_replace('\\', '/', $v);
  465. $v1 = str_replace(IA_ROOT . '/', '', $v);
  466. if ($subdir && is_dir($v)) {
  467. file_lists($v . '/', $subdir, $ex, $isdir, $md5);
  468. continue;
  469. }
  470. if (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) == $ex) {
  471. if ($md5) {
  472. $file_list[$v1] = md5_file($v);
  473. } else {
  474. $file_list[] = $v1;
  475. }
  476. continue;
  477. } elseif (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) != $ex) {
  478. unset($list[$k]);
  479. continue;
  480. }
  481. }
  482. return $file_list;
  483. }
  484. function file_remote_attach_fetch($url, $limit = 0, $path = '') {
  485. global $_W;
  486. $url = trim($url);
  487. if (empty($url)) {
  488. return error(-1, '文件地址不存在');
  489. }
  490. load()->func('communication');
  491. $resp = ihttp_get($url);
  492. if (is_error($resp)) {
  493. return error(-1, '提取文件失败, 错误信息: ' . $resp['message']);
  494. }
  495. if (intval($resp['code']) != 200) {
  496. return error(-1, '提取文件失败: 未找到该资源文件.');
  497. }
  498. $ext = $type = '';
  499. switch ($resp['headers']['Content-Type']) {
  500. case 'application/x-jpg':
  501. case 'image/jpg':
  502. case 'image/jpeg':
  503. $ext = 'jpg';
  504. $type = 'images';
  505. break;
  506. case 'image/png':
  507. $ext = 'png';
  508. $type = 'images';
  509. break;
  510. case 'image/gif':
  511. $ext = 'gif';
  512. $type = 'images';
  513. break;
  514. case 'video/mp4':
  515. case 'video/mpeg4':
  516. $ext = 'mp4';
  517. $type = 'videos';
  518. break;
  519. case 'video/x-ms-wmv':
  520. $ext = 'wmv';
  521. $type = 'videos';
  522. break;
  523. case 'audio/mpeg':
  524. $ext = 'mp3';
  525. $type = 'audios';
  526. break;
  527. case 'audio/mp4':
  528. $ext = 'mp4';
  529. $type = 'audios';
  530. break;
  531. case 'audio/x-ms-wma':
  532. $ext = 'wma';
  533. $type = 'audios';
  534. break;
  535. default:
  536. return error(-1, '提取资源失败, 资源文件类型错误.');
  537. break;
  538. }
  539. if (empty($path)) {
  540. $path = $type . "/{$_W['uniacid']}/" . date('Y/m/');
  541. } else {
  542. $path = parse_path($path);
  543. }
  544. if (!$path) {
  545. return error(-1, '提取文件失败: 上传路径配置有误.');
  546. }
  547. if (! is_dir(ATTACHMENT_ROOT . $path)) {
  548. if (! mkdirs(ATTACHMENT_ROOT . $path, 0700, true)) {
  549. return error(-1, '提取文件失败: 权限不足.');
  550. }
  551. }
  552. if (!$limit) {
  553. if ($type == 'images') {
  554. $limit = $_W['setting']['upload']['image']['limit'] * 1024;
  555. } else {
  556. $limit = $_W['setting']['upload']['audio']['limit'] * 1024;
  557. }
  558. } else {
  559. $limit = $limit * 1024;
  560. }
  561. if (intval($resp['headers']['Content-Length']) > $limit) {
  562. return error(-1, '上传的媒体文件过大(' . sizecount($resp['headers']['Content-Length']) . ' > ' . sizecount($limit));
  563. }
  564. $filename = file_random_name(ATTACHMENT_ROOT . $path, $ext);
  565. $pathname = $path . $filename;
  566. $fullname = ATTACHMENT_ROOT . $pathname;
  567. if (file_put_contents($fullname, $resp['content']) == false) {
  568. return error(-1, '提取失败.');
  569. }
  570. return $pathname;
  571. }
  572. function file_is_image($url) {
  573. if (!parse_path($url)) {
  574. return false;
  575. }
  576. $pathinfo = pathinfo($url);
  577. $extension = strtolower($pathinfo['extension']);
  578. return !empty($extension) && in_array($extension, array('jpg', 'jpeg', 'gif', 'png'));
  579. }
  580. function file_image_quality($src, $to_path, $ext) {
  581. load()->classs('image');
  582. global $_W;
  583. $quality = intval($_W['setting']['upload']['image']['zip_percentage']);
  584. if ($quality <= 0 || $quality >= 100) {
  585. return;
  586. }
  587. if (filesize($src) / 1024 > 5120) {
  588. return;
  589. }
  590. $result = Image::create($src, $ext)->saveTo($to_path, $quality);
  591. return $result;
  592. }