php5.4 以上版本废除@ 用 new CURLFile()代替
if(empty($urlinfo['host'])){
$tmp_name=dirname($file['tmp_name']).'/'.$file['title'].'.'.$file['extension'];
rename($file['tmp_name'],$tmp_name);
if(version_compare(phpversion(),'5.5.0') >= 0 && class_exists('CURLFile')){
$fields['file'] = new CURLFile(realpath($tmp_name));
}else{
$fields['file'] = '@'.$tmp_name;
}
}
转自:http://segmentfault.com/a/1190000000725185
相关demo
php_curl.php
<?php
/**
* Created by PhpStorm.
* User: kate
* Date: 2015/10/22
* Time: 12:59
*/
header('content-type:text/html;charset=utf8');
$ch = curl_init();
$data = array('savePath' => './upload/'.date('ymd').'/', 'img' => '@D:/wamp/www/Public/images/zu.png');//php5.4
//$data = array('savePath' => './upload/'.date('ymd').'/', 'img' => new CURLFile('D:/wamp/www/Public/images/zu.png'));//大于php5.4//$data = array('savePath' => './Uploads/'.date('ymd').'/', 'img' => '@/home/wwwroot/kjjdefault/Public/Uploads/20150916/thumb_55f9000bbfc6a.jpg');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/myTest/get_img.php');
curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$return = curl_exec($ch);
curl_close($ch);
var_dump($return);
get_img.php
<?php
/**
* Created by PhpStorm.
* User: kate
* Date: 2015/10/22
* Time: 13:20
*/
//print_r($_POST);
//print_r($_FILES);
if($_FILES){
$filename = $_FILES['img']['name'];
$tmpname = $_FILES['img']['tmp_name'];
$savePath = $_POST['savePath'];
if(!$savePath){
$savePath = './upload/'.date('ymd').'/';
}
if(!is_dir($savePath)) {
// 检查目录是否编码后的
if(is_dir(base64_decode($savePath))) {
$savePath = base64_decode($savePath);
}else{
// 尝试创建目录
if(!mkdir($savePath, 0777, true)){
$this->error = '上传目录'.$savePath.'不存在';
return false;
}
}
}else {
if(!is_writeable($savePath)) {
$this->error = '上传目录'.$savePath.'不可写';
return false;
}
}
if(move_uploaded_file($tmpname,$savePath.$filename)){
return json_encode(true);
}else{
$data = json_encode($_FILES);
echo $data;
}
}