zoukankan      html  css  js  c++  java
  • curl文件上传有两种方式,一种是post_fileds,一种是infile

    curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄!

    $ch curl_init();

    $data = array('name' => 'Foo''file' => '@/home/user/test.png');

    curl_setopt($chCURLOPT_URL'http://localhost/upload.php');
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS$data);

    curl_exec($ch);

    Note:

    传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded

     

      1. /* 
      2.  * To change this template, choose Tools | Templates 
      3.  * ftp curl方法操作类 
      4.  */  
      5. class ftp{  
      6.     //FTP服务器地址  
      7.     public static $host = "127.0.0.1";  
      8.     //FTP端口  
      9.     public static $port = "2121";  
      10.     //上传的FTP目录  
      11.     public static $uploaddir = "upblod";  
      12.     //读取的FTP目录  
      13.     public static $readdir = "read";  
      14.     //FTP用户名  
      15.     public static $usrname = "user";  
      16.     //FTP密码  
      17.     public static $pwd = "pwd";  
      18.     /* 
      19.      * curl 方法将文件上传到FTP服务器 
      20.      * $filename上传到FTP的文件名,$uploadfile具体需要上传文件的地址(我用的绝对路径) 
      21.      */  
      22.     public static function ftp_upload($filename,$uploadfile)  
      23.     {  
      24.         $url = "ftp://".self::$host.":".self::$port."/".self::$uploaddir."/".$filename;  
      25.         //需要上传的文件  
      26.         $fp = fopen ($uploadfile"r");   
      27.         $ch = curl_init();   
      28.         curl_setopt($ch, CURLOPT_VERBOSE, 1);  //有意外发生则报道   
      29.         curl_setopt($ch, CURLOPT_USERPWD, self::$usrname.':'.self::$pwd); //FTP登陆账号密码,模拟登陆   
      30.         curl_setopt($ch, CURLOPT_URL, $url);   
      31.         curl_setopt($ch, CURLOPT_PUT, 1); //用HTTP上传一个文件   
      32.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不输出   
      33.         curl_setopt($ch, CURLOPT_INFILE, $fp); //要上传的文件   
      34.         $http_result = curl_exec($ch); //执行   
      35.         $error = curl_error($ch);   
      36.         curl_close($ch);   
      37.         fclose($fp);  
      38.         //成功上传文件 返回true  
      39.         if (!$error)   
      40.         {   
      41.             return true;  
      42.         }  
      43.      }  
      44.     /* 
      45.      * curl 方法将读取FTP文件并保存在本地使用 
      46.      * $filenameFTP服务器文件名,$filepath 保存到本地(服务器)的目录 
      47.      */      
      48.     public static function ftp_read($filename,$filepath)  
      49.     {         
      50.         $curl = curl_init();   
      51.           
      52.         $target_ftp_file = "ftp://".self::$host.":".self::$port."/".self::$readdir."/".$filename;//完整路径  
      53.           
      54.         curl_setopt($curl, CURLOPT_URL,$target_ftp_file);  
      55.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
      56.         curl_setopt($curl, CURLOPT_VERBOSE, 1);  
      57.         curl_setopt($curl, CURLOPT_FTP_USE_EPSV, 0);  
      58.         curl_setopt($curl, CURLOPT_TIMEOUT, 300); // times out after 300s  
      59.         curl_setopt($curl, CURLOPT_USERPWD,self::$usrname.':'.self::$pwd);//FTP用户名:密码  
      60.         // Sets up the output file  
      61.         //本地保存目录  
      62.         if(is_dir($filepath)){  
      63.          $outfile = fopen($filepath.$filename'w');//保存到本地的文件名  
      64.          curl_setopt($curl,CURLOPT_FILE,$outfile);  
      65.          // Executes the cURL   
      66.          $info = curl_exec($curl);  
      67.          fclose($outfile);  
      68.          $error_no = curl_errno($curl);  
      69.          curl_close($curl);  
      70.          //成功读取文件,返回 true  
      71.          if($info){  
      72.              return true;  
      73.          }  
      74.         }          
      75.      }  
      76. }  
      77. ?> 
  • 相关阅读:
    scala java 混合编译配置
    hadoop自带RPC的使用 代码demo
    《Java多线程设计模式》学习
    b+tree(mongoDB索引,mysql 索引) LSM树(hbase ) Elasticsearch索引
    java jvm虚拟机类加载器
    java jvm虚拟机类加载过程
    凉拌麻辣鸡丝
    C#与C++区别-------转载自博客园-Wei_java
    2019.1.17-我不选ABCD,我选E
    2019.1.1-考研总结and如果二战怎么办
  • 原文地址:https://www.cnblogs.com/kudosharry/p/3730434.html
Copyright © 2011-2022 走看看