<?php /** * fastdfs扩展包 * @author whb * @version 1.1 (2013-03-28 18:10:21) */ class Util_Fastdfs { private $_fastdfs; /** * 初始化 */ public function __construct() { $this->_fastdfs = new MyFastDFSClient(0, true); //multi spread } /** * 上传文件 * * @param string $file_id * @param string $file_name * @return boolean */ public function upload($file_id, $file_name) { $file_id = $this->getFileId($file_id); if (!$this->_fastdfs->upload_by_filename($file_id, $file_name)) { return false; } /*if(!$this->exists($file_id)) { if (!$this->_fastdfs->upload_by_filename($file_id, $file_name)) { return false; } }*/ return true; } /** * 文件流上传 * * @param string $file_id * @param stream $stream * @return boolean */ public function uploadByStream($file_id, $stream) { $file_id = $this->getFileId($file_id); if(!$this->exists($file_id)) { if (!$this->_fastdfs->upload_by_filebuff($file_id, $stream)) { return false; } } return true; } /** * 错误信息 * * @return string */ public function errorInfo() { return $this->_fastdfs->get_last_error_info(); } /** * 错误代码 * * @return integer */ public function errorNumber() { return $this->_fastdfs->get_last_error_no(); } /** * 获取真实地址 * * @param string $file_id * @return string */ public function getRealPath($file_id) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->get_file_id($file_id); } /** * 判断文件是否存在 * * @param string $file_id * @return boolean */ public function exists($file_id) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->file_exists($file_id); } /** * 删除文件 * * @param string $file_id * @return boolean */ public function delete($file_id) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->delete_file($file_id); } /** * 读取文件 * * @param string $file_id * @return string */ public function read($file_id) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->download_file_to_buff($file_id); } /** * 读取到文件 * * @param string $file_id * @param string $filename * @return boolean */ public function readToFile($file_id, $filename) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->download_file_to_file($file_id, $filename); } /** * 修改文件内容 * * @param int $file_id * @param string $filename * @param int $offset * @return boolean */ public function modifyByFilename($file_id, $filename, $offset = 0) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->modify_by_filename($file_id, $filename, $offset); } /** * 根据文件流修改文件内容 * * @param int $file_id * @param stream $stream * @param int $offset * @return boolean */ public function modifyByStream($file_id, $stream, $offset = 0) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->modify_by_filebuff($file_id, $stream, $offset); } /** * 添加到文件 * * @param string $file_id * @param string $filename * @return boolean */ public function appendByFilename($file_id, $filename) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->append_by_filename($file_id, $filename); } /** * 文件流添加到文件 * * @param string $file_id * @param stream $stream * @return boolean */ public function appendByStream($file_id, $stream) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->append_by_filebuff($file_id, $stream); } /** * 截去内容 * * @param string $file_id * @param integer $size * @return boolean */ public function truncate($file_id, $size = 0) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->truncate_file($file_id, $size); } /** * 文件信息 * * @param string $file_id * @return array( * create_timestamp: the file create timestamp (unix timestamp) file_size: the file size (bytes) source_ip_addr: the source storage server ip address crc32: the crc32 signature of the file) */ public function fileInfo($file_id) { $file_id = $this->getFileId($file_id); return $this->_fastdfs->get_file_info($file_id); } /** * 获取文件大小 * * @param string $file_id * @return int */ public function filesize($file_id) { $file_id = $this->getFileId($file_id); return strlen($this->_fastdfs->read($file_id)); } /** * 获取file_id * * @param string $file_id * @return string */ public function getFileId($file_id) { return str_replace('//','/', $file_id); } /** * 关闭链接 * * @return boolean */ public function close() { return $this->_fastdfs->close(); } public function __destruct() { $this->close(); } }