Home  >  Article  >  Backend Development  >  php curl 上传文件代码实例,_PHP教程

php curl 上传文件代码实例,_PHP教程

WBOY
WBOYOriginal
2016-07-13 09:56:01848browse

php curl 上传文件代码实例,

假设server端上传文件处理脚本upload.php:
复制代码 代码如下:
 
print_r($_POST); 
print_r($_FILES); 

1、使用 CURL 默认的方法
复制代码 代码如下:
//如果php文件是utf8编码,系统是GBK编码,那么就需要转下编码,要不然Php在系统中找不到这个文件   
$file = realpath(mb_convert_encoding('测试图片.JPG','GBK','utf8')); 
 
$file = realpath('temp.jpg'); //要上传的文件   
$fields['f'] = '@'.$file; // 前面加@符表示上传图片  
 
$ch =curl_init(); 
 
 
curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php'); 
 
curl_setopt($ch,CURLOPT_POST,true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
 
 
$content = curl_exec($ch); 
 
echo $content; 

2、另类的做法,有时我们需要将动态产生的内容当做文件上传到远程服务器,却又不想在本地服务器中构建临时文件。这样就有了这个另类的写法

复制代码 代码如下:
$contents = 这里是文件内容,也可以是图片二进制,图片需要修改上传文件类型 
TEXT; 
 
$varname = 'my';//上传到$_FILES数组中的 key 
$name = '3.txt';//文件名 
$type = 'text/plain';//文件类型 
 
$key = "$varname\"; filename=\"$name\r\nContent-Type: $type\r\n"; 
$fields[$key] = $contents; 
 
 
 
$ch =curl_init(); 
 
 
curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php'); 
 
curl_setopt($ch,CURLOPT_POST,true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
 
 
$content = curl_exec($ch); 
 
echo $content; 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/990551.htmlTechArticlephp curl 上传文件代码实例, 假设server端上传文件处理脚本upload.php: 复制代码 代码如下: php print_r($_POST); print_r($_FILES); 1、使用 CURL 默认的方...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn