7-31 4,938 views
一、安装扩展
$ cd /soft/Fastdfs
$ cd php_client
$ php_client]#/usr/php/bin/phpize /*不一定是这个路径可以先 find / -name phpize */
$ ./configure --with-php-config=/usr/local/php/bin/php-config /*不一定是这个路径可以先 find / -name php-config*/
$ php_client]#make && make install
$ php_client]#cat fastdfs_client.ini >> /usr/local/php/etc/php.ini
首先要确保你有phpize 和 php-config ,如果没有参考 http://www.koukousky.com/back/php/1218.html 此文章。
输出phpinfo(); 查看 fastdfs ,下图 表示安装成功
二、测试
随便在服务器新建一个php文件,键入如下内容
出现5.01 。正是我的fastdfs的版本号。证明扩展安装成功并可调用方法!
三、php测试上传文件
首先准备图片文件。然后执行如下命令
<?php
echo fastdfs_client_version() . "\n";//版本号
echo 'fastdfs_tracker_make_all_connections result: ' . fastdfs_tracker_make_all_connections() . "\n";//测试链接
echo "<br>";
$tracker = fastdfs_tracker_get_connection();//获取一个tracker server连接:
//状态测试:(主要测试storage的状态)
if (!fastdfs_active_test($tracker))
{
error_log("fastdfs_active_test errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
exit(1);
}
$storage = fastdfs_tracker_query_storage_store();
if (!$storage)
{
error_log("fastdfs_tracker_query_storage_store errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
exit(1);
}
$server = fastdfs_connect_server($storage['ip_addr'], $storage['port']);
if (!$server)
{
error_log("fastdfs_connect_server errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
exit(1);
}
if (!fastdfs_active_test($server))
{
error_log("fastdfs_active_test errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
exit(1);
}
$storage['sock'] = $server['sock'];
//文件上传方法
$file_info = fastdfs_storage_upload_by_filename("/usr/local/nginx/html/123.jpg", null, array(), null, $tracker, $storage);
if ($file_info){
$group_name = $file_info['group_name'];
$remote_filename = $file_info['filename'];
var_dump($file_info);echo "<br>";
var_dump(fastdfs_get_file_info($group_name, $remote_filename));echo "<br>";
echo "file exist: " . fastdfs_storage_file_exist($group_name, $remote_filename) . "\n";//确认是否成功
echo $_SERVER['SERVER_NAME'] .'/' . $group_name .'/' . $remote_filename ;
}