/** * 上传图片api * */ public function upload(){ $result = array('code' => 0, 'message' => 'ok'); ini_set('upload_max_filesize', '20M'); ini_set('post_max_size', '20M'); ini_set('memory_limit', '128M'); //接收上传的远程地址 $url = isset($_POST['url']) ? trim($_POST['url']) : ''; if (empty($url)) { $result['code'] = 40001; $result['message'] = 'url不能为空'; Helper_Http::writeJson(200,$result); } $file = $_FILES['file']; if ($file['error'] != 0) { $result['code'] = 40001; $result['message'] = '上传出错'; Helper_Http::writeJson(200,$result); } else { $filename = $file['name']; $tmpfile = $file['tmp_name']; $filetype = $file['type']; $data = $this->upload_file($url, $filename, $tmpfile, $filetype); echo $data; die; } } /** * curl上传文件 * * @param unknown $url * @param unknown $filename * @param unknown $path * @param unknown $type */ function upload_file($url,$filename,$path,$type){ //php 5.5以上的用法 if (class_exists('\CURLFile')) { $data = array('file' => new \CURLFile(realpath($path),$type,$filename)); } else { $data = array( 'file'=>'@'.realpath($path).";type=".$type.";filename=".$filename ); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $return_data = curl_exec($ch); curl_close($ch); echo $return_data; } 远程代码的处理逻辑跟上传文件逻辑一样,这中间其实是有两部上传。 php上传文件,通过curl上传到远程服务器