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 安装

# cd /usr/local/src/fastdfs/FastDFS/php_client
# /usr/local/php5/bin/phpize 
# ./configure --with-php-config=/usr/local/php5/bin/php-config  
# make && make install  
# cat fastdfs_client.ini >> /usr/local/php5/etc/php.ini

2.2 查看是否安装成功

# php -m | grep fastdfs_client

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


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

# pkill php-fpm
# /usr/local/php5/sbin/php-fpm


3 通过http上传

3.1 上传页面代码

test.php







3.2 接收文件php

upload.php

getPostFilename());                                              
    
    $ret['file'] = $file;
    $ret['fileId'] = uploadToFastdfs($curlFile, $fileSuffix);                                                        
    return $ret;
}/*}}}*/                                                                                                  

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

//上传文件到fastdfs
function uploadToFastdfs(CurlFile $file, $fileSuffix)                                                  
{/*{{{*/                                                                                                  
    $fdfs = new FastDFS(); 
    $tracker = $fdfs->tracker_get_connection();  
    $fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);  
    $fdfs->tracker_close_all_connections();    
    return $fileId;
}/*}}}*/                                                                                                  

function start()
{
    $ret = uploadAttach();  
    print_r($ret);
}
start();
?>


3.3 上传一张图片

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


3.4 上传结果

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


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

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


4 curl上传

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

curlupload.php

 0, 'errmsg' => '', 'result' => '');

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

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

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

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

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

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

    // initialize result post array
    $result_post = array();

    //generate post string
    $post_array = array();
    $post_strings = array();
    if (!is_array($post_data)) {
        $result['errno'] = 5;
        $result['errmsg'] = 'Params error.';
        return json_encode($result);
        // return false;
    }

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

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

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

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

    // set post data
    $result_post = array_merge($post_array, $file_fields);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $result_post);
    // print_r($result_post);

    //and finally send curl request
    $output = curl_exec($ch);
    $result['result'] = $output;
    // print_r($result);

    if (curl_errno($ch )) {       
        $result['errno'] = curl_errno($ch);
        $result['errmsg'] = curl_error($ch);
        // return false;
    } else {
        // return $result;
    }
    curl_close($ch);
    return $result;
}/*}}}*/

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

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

    print_r($ret);
}

start();
?>


打印结果

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


你可能感兴趣的:(Linux常用软件安装与配置)