php+curl上传文件到远程服务器
一、window下安装php_curl支持
1.将PHP安装文件夹下的三个文件php_curl.dll(可能在ext文件夹中),libeay32.dll,ssleay32.dll 复制到 %windir%/system32下;
2.打开php.ini(可能在PHP环境的安装目录下,默认在c:\WINDOWS目录下),将;extension=php_curl.dll头部的分号去掉;
3.重启php的服务(apache或者IIS),如重启IIS,运行iisreset即可。
二、注意事项。
注意服务器文件夹的权限,
php.ini 里 upload_tmp_dir 设置一个全新的目录 也赋予Everyone 读写权限。
上传文件的目标目录也赋予Everyone 读写权限。
三、直接上代码
1、文件curlTest.php:
<form action=" curlTest.php" enctype="multipart/form-data" method="post"> <p>UpLoad: <input type="file" name="file" id="file"/></p> <input type="submit" value="Submit" /> </form> <?php upload(); /** * 上传图片api * */ 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']) : ''; $url ="www.123.com/k1/upload.php"; if (empty($url)) { $result['code'] = 40001; $result['message'] = 'url不能为空'; //Helper_Http::writeJson(200,$result); } $file = $_FILES['file']; print_r($_FILES); if ($file['error'] != 0) { $result['code'] = 40001; $result['message'] = '上传出错'; //Helper_Http::writeJson(200,$result); echo 1; } else { $filename = $file['name']; $tmpfile = $file['tmp_name']; $filetype = $file['type']; $data = upload_file($url, $filename, $tmpfile, $filetype); echo $data; print_r ($url); die; } } /** * curl上传文件 * * @param unknown $url * @param unknown $filename * @param unknown $path * @param unknown $type */ function upload_file($url,$filename,$path,$type){ echo 3; echo $path; //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 ); } echo 'data:'; print_R($data); $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; } ?>
2、文件upload.php:
<?php echo "<pre> "; print_r($_FILES); $uploaddir = 'D:/temp/caches/'; $uploadfile = $uploaddir . basename($_FILES['file']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; ?>
http://my.oschina.net/forMemory/blog/374451
http://my.oschina.net/adamboy/blog/54436
http://bbs.csdn.net/topics/300018585
http://blog.csdn.net/cyuyan112233/article/details/21007163
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。