接口上传有大小限制,经过测试,基本上5M以内的都可以上传。
public function upload($file) { if (is_uploaded_file($file['tmp_name'])) { $arr = pathinfo($file['name']); $ext_suffix = $arr['extension']; $allow_suffix = array('jpg', 'gif', 'jpeg', 'png'); if (!in_array($ext_suffix, $allow_suffix)) { $this->msg(['code' => 1, 'msg' => '上传格式不支持']); } $new_filename = time() . rand(100, 1000) . '.' . $ext_suffix; if (move_uploaded_file($file['tmp_name'], $new_filename)) { $data = $this->upload2('https://kfupload.alibaba.com/mupload', $new_filename); $pattern = '/"url":"(.*?)"/'; preg_match($pattern, $data, $match); @unlink($new_filename); if ($match && $match[1] != '') { $this->msg(['code' => 0, 'msg' => $match[1]]); } else { $this->msg(['code' => 1, 'msg' => '上传失败']); } } else { $this->msg(['code' => 1, 'msg' => '上传数据有误']); } } else { $this->msg(['code' => 1, 'msg' => '上传数据有误']); } } public function upload2($url, $file) { return $this->get_url($url, [ 'scene' => 'aeMessageCenterImageRule', 'name' => $file, 'file' => new \CURLFile(realpath($file)), ]); } public function get_url($url, $post) { $ch = curl_init(); curl_setopt($ch, CURLOPT_TIMEOUT,0); //超时设置时间为0是无限等待,因为有些服务器带宽炒鸡小,所以最好还是把这个给加上去,不然图片没上传完,就结束了这个请求,就是失败了 curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } if (curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } $result = curl_exec($ch); curl_close($ch); return $result; } public function msg($data) { exit(json_encode($data)); }
完整源文件请直接点击下面面板下载:
文件下载 | 文件名称:阿里图床源码 | 文件大小:84.0 KB |
下载声明:本站文件大多来自于网络,仅供学习和研究使用,不得用于商业用途,如有版权问题,请联系博猪! | ||
下载地址:本站下载 |
开源项目地址:GIT项目开源-阿里云图床