/** * 手机端照片上传 * @param array $contentData 二进制数据 * @param array $infoData * @param string $type 上传类型 * @param string $flag 上传标识(1日记,2广播,3联系人,4头像,5群组封面) */ public function upload($contentData, $infoData, $type, $flag) { $user_id = $this->uid; if ($type = 'wp') { //获取相册ID $album_id = $this->db->getOne("wp_album", "album_id", "user_id = {$user_id} AND album_flag = {$flag}"); if (!$album_id) { $data = array("album_name" => '广播相册', "user_id" => $user_id, "create_dt" => time(), "update_dt" => time(), "album_flag" => $flag); $album_id = $this->db->insertData("wp_album", $data); } $file_md5 = md5($contentData); //上传到FS include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); //FS存储地址 $img_fs_name = $this->fs->upByBuff($contentData, 'jpg'); //写TTServer $value = array("pic_fs_path" => $img_fs_name, "exif" => "", "height" => $infoData['height'], "width" => $infoData['width']); $this->TT = new TTServer(); $this->TT->set($file_md5, $value); //插入wp表数据 $pic_data_array['album_id'] = $album_id; $pic_data_array['user_id'] = $user_id; $pic_data_array['create_time'] = time(); $pic_data_array['update_time'] = time(); $pic_data_array['upload_ip'] = $_SERVER['REMOTE_ADDR']; $pic_data_array['upload_file_name'] = 'sj.jpg'; $pic_data_array['file_md5'] = $file_md5; $pic_data_array['file_size'] = $infoData['size']; $pic_data_array['file_type'] = $infoData['type']; $pic_data_array['pic_width'] = $infoData['width']; $pic_data_array['pic_height'] = $infoData['height']; $pic_id = $this->db->insertData("wp_pic", $pic_data_array); if ($pic_id) { $fid = $pic_id + 100000; if ($flag == 2) { //广播返回图片地址不同 return array("data" => Kohana::config('album.recordThumb') . 'imgs/' . $pic_id . '_160.jpg', "fid" => $fid, "md5" => $file_md5); } else { return array("data" => Kohana::config('album.thumb') . 'imgs/' . $pic_id . '_160.jpg', "fid" => $fid, "md5" => $file_md5); } } else { return null; } } }
echo "fastdfs_storage_append_by_filename fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info($group_name, $appender_filename)); if (!fastdfs_storage_modify_by_filename("/usr/include/stdlib.h", 0, $group_name, $appender_filename)) { echo "fastdfs_storage_modify_by_filename fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info($group_name, $appender_filename)); if (!fastdfs_storage_truncate_file($group_name, $appender_filename, 0)) { echo "fastdfs_storage_truncate_file fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info($group_name, $appender_filename)); echo "function test done\n\n"; $fdfs = new FastDFS(); $file_info = $fdfs->storage_upload_appender_by_filename("/usr/include/stdio.h"); if (!$file_info) { echo "{$fdfs->storage_upload_appender_by_filename} fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; exit; } $group_name = $file_info['group_name']; $remote_filename = $file_info['filename']; var_dump($file_info); $file_id = "{$group_name}/{$remote_filename}"; var_dump($fdfs->get_file_info($group_name, $remote_filename)); $appender_filename = $remote_filename; echo "file id: {$group_name}/{$appender_filename}\n"; if (!$fdfs->storage_append_by_filename("/usr/include/stdlib.h", $group_name, $appender_filename)) { echo "{$fdfs->storage_append_by_filename} fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; exit;
echo "{$slave_file_id}\n != \n{$generated_file_id}\n"; } echo "delete file {$slave_file_id} return: " . fastdfs_storage_delete_file1($slave_file_id) . "\n"; } else { echo "fastdfs_storage_upload_slave_by_filebuff1 fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; } echo "delete file {$file_id} return: " . fastdfs_storage_delete_file1($file_id) . "\n"; } var_dump(fastdfs_tracker_query_storage_update($group_name, $remote_filename)); var_dump(fastdfs_tracker_query_storage_fetch($group_name, $remote_filename)); var_dump(fastdfs_tracker_query_storage_list($group_name, $remote_filename)); var_dump(fastdfs_tracker_query_storage_update1($file_id)); var_dump(fastdfs_tracker_query_storage_fetch1($file_id, $tracker)); var_dump(fastdfs_tracker_query_storage_list1($file_id, $tracker)); echo "fastdfs_tracker_close_all_connections result: " . fastdfs_tracker_close_all_connections() . "\n"; $fdfs = new FastDFS(); echo 'tracker_make_all_connections result: ' . $fdfs->tracker_make_all_connections() . "\n"; $tracker = $fdfs->tracker_get_connection(); var_dump($tracker); $server = $fdfs->connect_server($tracker['ip_addr'], $tracker['port']); var_dump($server); var_dump($fdfs->disconnect_server($server)); var_dump($fdfs->tracker_query_storage_store_list()); //var_dump($fdfs->tracker_list_groups()); //var_dump($fdfs->tracker_query_storage_store()); //var_dump($fdfs->tracker_query_storage_update($group_name, $remote_filename)); //var_dump($fdfs->tracker_query_storage_fetch($group_name, $remote_filename)); //var_dump($fdfs->tracker_query_storage_list($group_name, $remote_filename)); var_dump($fdfs->tracker_query_storage_update1($file_id)); var_dump($fdfs->tracker_query_storage_fetch1($file_id)); var_dump($fdfs->tracker_query_storage_list1($file_id));
public function getAvatar($user_id, $size = 'middle') { $user_id = $user_id ? $user_id : $this->uid; if ($size == 'small') { $type = 48; } else { $type = 120; } //获取用户头像照相册ID $result = $this->db->getRow("album_user_album", "album_id", "album_default = '2' AND user_id={$user_id}"); $album_id = $result['album_id']; if (!$album_id) { return null; } //获取图片md5 $file_md5 = $this->db->getOne("album_pic", "file_md5", "album_id={$album_id} AND is_avatar = '1' ORDER BY pic_id DESC"); if ($file_md5) { //获取FS地址 $this->TT = new TTServer(); $pic_fs = $this->TT->get($file_md5); if ($pic_fs['pic_fs_path']) { //获取缩略图的FS路径 include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); $fsfile_name_array = explode('.', $pic_fs['pic_fs_path']); $thumb_path = $fsfile_name_array[0] . '_' . $type . '.' . $fsfile_name_array[1]; //获取文件数据流 $thumb = $this->fs->downToBuff($thumb_path); $file_size = strlen($thumb); if ($file_size) { return $thumb; } else { return null; } } } else { //没有设置头像,返回默认头像 return null; } }
$remote_filename = $file_info['filename']; var_dump($file_info); var_dump(fastdfs_get_file_info($group_name, $remote_filename)); fastdfs_storage_download_file_to_callback($group_name, $remote_filename, $download_callback_array); } else { echo "upload file fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; } $file_id = fastdfs_storage_upload_by_callback1($upload_callback_array, 'txt'); if ($file_id) { var_dump($file_id); $download_callback_arg['filename'] = '/tmp/out1.txt'; fastdfs_storage_download_file_to_callback1($file_id, $download_callback_array); } else { echo "upload file fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; } $fdfs = new FastDFS(); $file_info = $fdfs->storage_upload_by_callback($upload_callback_array, 'txt'); if ($file_info) { $group_name = $file_info['group_name']; $remote_filename = $file_info['filename']; var_dump($file_info); var_dump($fdfs->get_file_info($group_name, $remote_filename)); $download_callback_arg['filename'] = '/tmp/fdfs_out.txt'; $fdfs->storage_download_file_to_callback($group_name, $remote_filename, $download_callback_array); } else { echo "upload file fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; } $file_id = $fdfs->storage_upload_by_callback1($upload_callback_array, 'txt'); if ($file_id) { var_dump($file_id); $download_callback_arg['filename'] = '/tmp/fdfs_out1.txt';
echo "fastdfs_storage_append_by_filename1 fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info1($appender_file_id)); if (!fastdfs_storage_modify_by_filename1("/usr/include/stdlib.h", 0, $appender_file_id)) { echo "fastdfs_storage_modify_by_filename1 fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info1($appender_file_id)); if (!fastdfs_storage_truncate_file1($appender_file_id, 0)) { echo "fastdfs_storage_truncate_file1 fail, errno: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info() . "\n"; exit; } var_dump(fastdfs_get_file_info1($appender_file_id)); echo "function test done\n\n"; $fdfs = new FastDFS(); $appender_file_id = $fdfs->storage_upload_appender_by_filename1("/usr/include/stdio.h"); if (!$appender_file_id) { echo "\$fdfs->storage_upload_appender_by_filename1 fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; exit; } var_dump($appender_file_id); var_dump($fdfs->get_file_info1($appender_file_id)); if (!$fdfs->storage_append_by_filename1("/usr/include/stdlib.h", $appender_file_id)) { echo "\$fdfs->storage_append_by_filename1 fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; exit; } var_dump($fdfs->get_file_info1($appender_file_id)); if (!$fdfs->storage_modify_by_filename1("/usr/include/stdlib.h", 0, $appender_file_id)) { echo "\$fdfs->storage_modify_by_filename1 fail, errno: " . $fdfs->get_last_error_no() . ", error info: " . $fdfs->get_last_error_info() . "\n"; exit;
public function uploadFileEnd($temp_id, $flag) { $uid = $this->uid; $data = $this->db->getRow("album_upload_temp", "file_title, file_md5, file_size, file_offset, file_path, file_type", "temp_id={$temp_id} AND user_id={$uid}"); if (!$data) { return array("code" => 403, "msg" => "uploadID不存在或无权限"); } //if($data['file_size'] != $data['file_offset']) { // return array("code" => 403, "msg" => "文件大小不匹配."); //} //$tempfsize = 0; //if( file_exists(ALBUM_TEMP.$data['file_path']) ) { // $tempfsize = filesize(ALBUM_TEMP.$data['file_path']); //} //if(!$tempfsize || $tempfsize != $data['file_size']) { // return array("code" => 403, "msg" => "文件大小不匹配!"); //} //移动临时文件到文件目录 if (!is_dir(ALBUM_IMAGE . $uid)) { mkdir(ALBUM_IMAGE . $uid, 0777); } //临时文件移动到user文件夹下 $tempfname = substr($data['file_path'], 0, -4); rename(ALBUM_TEMP . $data['file_path'], ALBUM_IMAGE . $tempfname); //移动文件成功 if (file_exists(ALBUM_IMAGE . $tempfname)) { $file_url = ALBUM_IMAGE . $tempfname; //获取图片的EXIF信息 $exif = Exif::get_exif_info($file_url); $img_exif = $exif['exif_info']; $degree = $exif['degree']; //获取图片的高度、宽度 $img = getimagesize($file_url); $width = $img[0]; $height = $img[1]; $file_format = $img[2]; if (!$img || !$width || !$height || $file_format > 6) { return false; } if ($file_format == 2 && $degree) { //旋转图片 $rotateDeg = $degree * 90; Image::rotate_by_degree($file_url, $rotateDeg); if ($degree == 1 || $degree == 3) { $t = $width; $width = $height; $height = $t; } $img_exif['degree'] = 0; $degree = 0; } $flag = $flag ? $flag : 2; //广播标识 $album_id = $this->db->getOne("wp_album", "album_id", "user_id = {$uid} AND album_flag={$flag}"); if (!$album_id) { //创建广播相册 $album_data = array('album_name' => '广播相册', 'user_id' => $uid, 'create_dt' => time(), 'update_dt' => time(), 'album_flag' => $flag); $album_id = $this->db->insertData("wp_album", $album_data); } $pic_info = array('album_id' => $album_id, 'user_id' => $uid, 'create_time' => time(), 'update_time' => time(), 'upload_ip' => $_SERVER['REMOTE_ADDR'], 'file_md5' => $data['file_md5'], 'file_size' => $data['file_size'], 'file_type' => $data['file_type'], 'pic_width' => $width, 'pic_height' => $height, 'degree' => intval($degree), 'appid' => 1); //上传到FS include 'application/include/FastDFS.php'; $this->fs = FastDFS::factory('group1'); //FS存储地址 $img_fs_name = $this->fs->upByFileName($file_url); //写TTServer $value = array("pic_fs_path" => $img_fs_name, "exif" => serialize($img_exif), "width" => $width, "height" => $height); $this->TT = new TTServer(); $this->TT->set($data['file_md5'], $value); $pic_id = $this->db->insertData("wp_pic", $pic_info); $this->db->query("UPDATE wp_album SET pic_num = pic_num+1 WHERE album_id={$album_id}"); //删除临时表数据 $this->db->DeleteData("album_upload_temp", array("temp_id" => $temp_id)); //生成160 $images = Image::createThumb_by_type($file_url, 160); $thumb_fs_name = substr($img_fs_name, 0, -4); $meta = array('remote_filename' => $thumb_fs_name . '_160.jpg'); $this->fs->upByFileName($images, $meta); if (file_exists($images)) { unlink($images); } return array("pid" => $pic_id, "url" => Kohana::config('album.recordThumb') . 'imgs/' . $pic_id . '_160.jpg', "uploaded" => true); } else { //移动文件失败 return null; } }