欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php+curl上传文件到远程服务器

程序员文章站 2022-05-15 22:55:16
...
一、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:


UpLoad:

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:

 "; print_r($_FILES);  $uploaddir = 'D:/temp/caches/'; $uploadfile = $uploaddir . basename($_FILES['file']['name']);  echo '
'; 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://bbs.csdn.net/topics/300018585