fastDFS中使用php上传文件 -- http上传与下载图片

CleverCode研究完fastDFS后,尝试着使用php上传文件到fastdfs。

1 fastDFS安装

  fastdfs分布式架构配置参考:http://blog.csdn.net/clevercode/article/details/52267080。

  fastdfs使用nginx配置参考:http://blog.csdn.net/clevercode/article/details/52276169。

   fastdfs缩略图生成参考:http://blog.csdn.net/clevercode/article/details/52278482。


2 fastDFS中php扩展的安装

 2.1 安装

[plain]  view plain  copy
  1. # cd /usr/local/src/fastdfs/FastDFS/php_client  
  2. # /usr/local/php5/bin/phpize   
  3. # ./configure --with-php-config=/usr/local/php5/bin/php-config    
  4. # make && make install    
  5. # cat fastdfs_client.ini >> /usr/local/php5/etc/php.ini  

2.2 查看是否安装成功

# php -m | grep fastdfs_client



2.3 配置fastDFS的client.conf

# vi /etc/fdfs/client.conf

tracker_server=192.168.101.135:22122  
http.tracker_server_port=80 


2.4重启pkill php-fpm

[plain]  view plain  copy
  1. # pkill php-fpm  
  2. # /usr/local/php5/sbin/php-fpm  


3 通过http上传

3.1 上传页面代码

test.php


[html]  view plain  copy
  1. <html>  
  2. <body>  
  3.   
  4. <form action="upload.php" method="post" enctype="multipart/form-data">  
  5.     <label for="file">Filename:</label>  
  6.     <input type="file" name="upFile" id="upFile" />   
  7.     <br />  
  8.     <input type="submit" name="submit" value="Submit" />  
  9. </form>  
  10.   
  11. </body>  
  12. </html>  

3.2 接收文件php

upload.php

[php]  view plain  copy
  1. <?php  
  2.   
  3. //上传附件  
  4. function uploadAttach()                                                                              
  5. {/*{{{*/                                                                                                    
  6.     $ret = array();  
  7.     $ret['errorcode'] = 0;  
  8.     $ret['errormsg'] = '';  
  9.     if(!$_FILES || false == isset($_FILES["upFile"]))  
  10.     {  
  11.         $ret['errorcode'] = 1;  
  12.         $ret['errormsg'] = "ERROR:upFile is not set";  
  13.         return $ret;  
  14.     }  
  15.   
  16.     $file = $_FILES["upFile"];  
  17.     if (false == isset($file['tmp_name']) || false == is_file($file['tmp_name']))  
  18.     {  
  19.         $ret['errorcode'] = 2;  
  20.         $ret['errormsg'] = "tmp_name is not file";  
  21.         return $ret;  
  22.     }  
  23.     if (0 == filesize($file['tmp_name']))  
  24.     {  
  25.         $ret['errorcode'] = 3;  
  26.         $ret['errormsg'] = "tmp_name filesize is 0";  
  27.         return $ret;  
  28.     }  
  29.   
  30.     $curlFile = new CurlFile($file['tmp_name'], $file['type'], $file['name']);    
  31.     $fileSuffix = getSuffix($curlFile->getPostFilename());                                                
  32.       
  33.     $ret['file'] = $file;  
  34.     $ret['fileId'] = uploadToFastdfs($curlFile$fileSuffix);                                                          
  35.     return $ret;  
  36. }/*}}}*/                                                                                                    
  37.   
  38. //获取后缀  
  39.  function getSuffix($fileName)   
  40.  {/*{{{*/  
  41.      preg_match('/\.(\w+)?$/'$fileName$matchs);  
  42.      return isset($matchs[1])?$matchs[1]:'';  
  43.  }/*}}}*/  
  44.   
  45. //上传文件到fastdfs  
  46. function uploadToFastdfs(CurlFile $file$fileSuffix)                                                    
  47. {/*{{{*/                                                                                                    
  48.     $fdfs = new FastDFS();   
  49.     $tracker = $fdfs->tracker_get_connection();    
  50.     $fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);    
  51.     $fdfs->tracker_close_all_connections();      
  52.     return $fileId;  
  53. }/*}}}*/                                                                                                    
  54.   
  55. function start()  
  56. {  
  57.     $ret = uploadAttach();    
  58.     print_r($ret);  
  59. }  
  60. start();  
  61. ?>  


3.3 上传一张图片



3.4 上传结果



3.5 访问(下载) http://192.168.101.132/group1/M00/00/00/wKhlhVfBiu2AWrzoAAKp3t_hiGI748.png



4 curl上传

  3 的http上传方式,需要在每一台php服务器上都按装fastdfs的php扩展。这里通过curl方式,直接上传到具有fastdfs扩展的php服务器上。

curlupload.php

[php]  view plain  copy
  1. <?php  
  2. function curl_multipart_post($url$post_data = array(), $file_fields = array(), $timeout=30)  
  3. {/*{{{*/  
  4.   
  5.     $result = array('errno' => 0, 'errmsg' => '''result' => '');  
  6.   
  7.     $ch = curl_init();  
  8.     //set various curl options first  
  9.   
  10.     // set url to post to  
  11.     curl_setopt($ch, CURLOPT_URL, $url);  
  12.   
  13.     // return into a variable rather than displaying it  
  14.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  15.   
  16.     //set curl function timeout to $timeout  
  17.     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
  18.     curl_setopt($ch, CURLOPT_VERBOSE, false);  
  19.   
  20.     //set method to post  
  21.     curl_setopt($ch, CURLOPT_POST, true);  
  22.   
  23.     // disable Expect header  
  24.     // hack to make it working  
  25.     $headers = array("Expect: ");  
  26.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
  27.   
  28.     // initialize result post array  
  29.     $result_post = array();  
  30.   
  31.     //generate post string  
  32.     $post_array = array();  
  33.     $post_strings = array();  
  34.     if (!is_array($post_data)) {  
  35.         $result['errno'] = 5;  
  36.         $result['errmsg'] = 'Params error.';  
  37.         return json_encode($result);  
  38.         // return false;  
  39.     }  
  40.   
  41.     foreach($post_data as $key=>$value) {  
  42.         $post_array[$key] = $value;  
  43.         $post_strings[] = urlencode($key)."=".urlencode($value);  
  44.     }  
  45.   
  46.     $post_string = implode("&"$post_strings);  
  47.   
  48.     // set post string  
  49.     // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
  50.   
  51.     // set multipart form data - file array field-value pairs  
  52.     if (!empty($file_fields)) {  
  53.         foreach($file_fields as $key => $value) {  
  54.             if (strpos(PHP_OS, "WIN") !== false) {  
  55.                 $value = str_replace("/""\\"$value); // win hack  
  56.             }  
  57.             $file_fields[$key] = "@".$value;  
  58.         }  
  59.     }  
  60.   
  61.     // set post data  
  62.     $result_post = array_merge($post_array$file_fields);  
  63.     curl_setopt($ch, CURLOPT_POSTFIELDS, $result_post);  
  64.     // print_r($result_post);  
  65.   
  66.     //and finally send curl request  
  67.     $output = curl_exec($ch);  
  68.     $result['result'] = $output;  
  69.     // print_r($result);  
  70.   
  71.     if (curl_errno($ch )) {         
  72.         $result['errno'] = curl_errno($ch);  
  73.         $result['errmsg'] = curl_error($ch);  
  74.         // return false;  
  75.     } else {  
  76.         // return $result;  
  77.     }  
  78.     curl_close($ch);  
  79.     return $result;  
  80. }/*}}}*/  
  81.   
  82. function start()  
  83. {  
  84.     $url = 'http://192.168.101.132/upload.php';  
  85.     $post = array('signature' => 123456);  
  86.     $fileFields = array('upFile' => '/data0/webRoot/upload.php');  
  87.   
  88.     $ret = curl_multipart_post($url,$post,$fileFields);  
  89.   
  90.     print_r($ret);  
  91. }  
  92.   
  93. start();  
  94. ?>  


打印结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值