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

1 fastDFS安装

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

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


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. <form action="upload.php" method="post" enctype="multipart/form-data">  
  4.     <label for="file">Filename:</label>  
  5.     <input type="file" name="upFile" id="upFile" />   
  6.     <br />  
  7.     <input type="submit" name="submit" value="Submit" />  
  8. </form>  

  9. </body>  
  10. </html>  


3.2 接收文件php

upload.php

[php]​ 

​view plain​

 ​​copy​



  1. <?php  

  2. //上传附件  
  3. function uploadAttach()                                                                              
  4. {/*{{{*/                                                                                                    
  5.     $ret = array();  
  6.     $ret['errorcode'] = 0;  
  7.     $ret['errormsg'] = '';  
  8.     if(!$_FILES || false == isset($_FILES["upFile"]))  
  9.     {  
  10.         $ret['errorcode'] = 1;  
  11.         $ret['errormsg'] = "ERROR:upFile is not set";  
  12.         return $ret;  
  13.     }  

  14.     $file = $_FILES["upFile"];  
  15.     if (false == isset($file['tmp_name']) || false == is_file($file['tmp_name']))  
  16.     {  
  17.         $ret['errorcode'] = 2;  
  18.         $ret['errormsg'] = "tmp_name is not file";  
  19.         return $ret;  
  20.     }  
  21.     if (0 == filesize($file['tmp_name']))  
  22.     {  
  23.         $ret['errorcode'] = 3;  
  24.         $ret['errormsg'] = "tmp_name filesize is 0";  
  25.         return $ret;  
  26.     }  

  27.     $curlFile = new CurlFile($file['tmp_name'], $file['type'], $file['name']);    
  28.     $fileSuffix = getSuffix($curlFile->getPostFilename());                                                

  29.     $ret['file'] = $file;  
  30.     $ret['fileId'] = uploadToFastdfs($curlFile, $fileSuffix);                                                          
  31.     return $ret;  
  32. }/*}}}*/                                                                                                    

  33. //获取后缀  
  34.  function getSuffix($fileName)   
  35.  {/*{{{*/  
  36.      preg_match('/\.(\w+)?$/', $fileName, $matchs);  
  37.      return isset($matchs[1])?$matchs[1]:'';  
  38.  }/*}}}*/  

  39. //上传文件到fastdfs  
  40. function uploadToFastdfs(CurlFile $file, $fileSuffix)                                                    
  41. {/*{{{*/                                                                                                    
  42.     $fdfs = new FastDFS();   
  43.     $tracker = $fdfs->tracker_get_connection();    
  44.     $fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);    
  45.     $fdfs->tracker_close_all_connections();      
  46.     return $fileId;  
  47. }/*}}}*/                                                                                                    

  48. function start()  
  49. {  
  50.     $ret = uploadAttach();    
  51.     print_r($ret);  
  52. }  
  53. start();  
  54. ?>  



3.3 上传一张图片

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


3.4 上传结果

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


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

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


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.     $result = array('errno' => 0, 'errmsg' => '', 'result' => '');  

  5.     $ch = curl_init();  
  6.     //set various curl options first  

  7.     // set url to post to  
  8.     curl_setopt($ch, CURLOPT_URL, $url);  

  9.     // return into a variable rather than displaying it  
  10.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  

  11.     //set curl function timeout to $timeout  
  12.     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
  13.     curl_setopt($ch, CURLOPT_VERBOSE, false);  

  14.     //set method to post  
  15.     curl_setopt($ch, CURLOPT_POST, true);  

  16.     // disable Expect header  
  17.     // hack to make it working  
  18.     $headers = array("Expect: ");  
  19.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  

  20.     // initialize result post array  
  21.     $result_post = array();  

  22.     //generate post string  
  23.     $post_array = array();  
  24.     $post_strings = array();  
  25.     if (!is_array($post_data)) {  
  26.         $result['errno'] = 5;  
  27.         $result['errmsg'] = 'Params error.';  
  28.         return json_encode($result);  
  29.         // return false;  
  30.     }  

  31.     foreach($post_data as $key=>$value) {  
  32.         $post_array[$key] = $value;  
  33.         $post_strings[] = urlencode($key)."=".urlencode($value);  
  34.     }  

  35.     $post_string = implode("&", $post_strings);  

  36.     // set post string  
  37.     // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  

  38.     // set multipart form data - file array field-value pairs  
  39.     if (!empty($file_fields)) {  
  40.         foreach($file_fields as $key => $value) {  
  41.             if (strpos(PHP_OS, "WIN") !== false) {  
  42.                 $value = str_replace("/", "\\", $value); // win hack  
  43.             }  
  44.             $file_fields[$key] = "@".$value;  
  45.         }  
  46.     }  

  47.     // set post data  
  48.     $result_post = array_merge($post_array, $file_fields);  
  49.     curl_setopt($ch, CURLOPT_POSTFIELDS, $result_post);  
  50.     // print_r($result_post);  

  51.     //and finally send curl request  
  52.     $output = curl_exec($ch);  
  53.     $result['result'] = $output;  
  54.     // print_r($result);  

  55.     if (curl_errno($ch )) {         
  56.         $result['errno'] = curl_errno($ch);  
  57.         $result['errmsg'] = curl_error($ch);  
  58.         // return false;  
  59.     } else {  
  60.         // return $result;  
  61.     }  
  62.     curl_close($ch);  
  63.     return $result;  
  64. }/*}}}*/  

  65. function start()  
  66. {  
  67.     $url = 'http://192.168.101.132/upload.php';  
  68.     $post = array('signature' => 123456);  
  69.     $fileFields = array('upFile' => '/data0/webRoot/upload.php');  

  70.     $ret = curl_multipart_post($url,$post,$fileFields);  

  71.     print_r($ret);  
  72. }  

  73. start();  
  74. ?>  



打印结果

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