一将文件存到项目路径下,数据库保存图片的路径
多文件上传思路是,上传一个文件服务器上同时入库表,然后会callback返回文件信息,js将文件ID插入input fileId,提交整个表单的时候,做sqlupdate将文件的from_id,from_table关联上。
设计优点:根据from_id,from_table可以很方便的判断服务器上的文件哪些是无用的临时文件,哪些是用户的有用文件,可以定期做文件清理操作
CREATE TABLE `dj_attachment` (
`file_id` bigint(11) NOT NULL AUTO_INCREMENT,
`from_table` varchar(30) NOT NULL DEFAULT '' COMMENT '业务分类',
`from_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '分类ID。为0是临时文件可删除',
`input_name` varchar(20) NOT NULL DEFAULT '' COMMENT '表单input',
`file_name` varchar(90) NOT NULL DEFAULT '' COMMENT '文件原名',
`file_size` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '文件大小',
`downloads` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '下载次数',
`save_path` varchar(200) NOT NULL DEFAULT '' COMMENT '上传的文件',
`oss_media_id` varchar(50) DEFAULT NULL COMMENT '云空间Api文件ID,常为字符串',
`is_oss` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否云空间文件 0本地文件 1云空间',
`thumb_path` varchar(200) DEFAULT NULL COMMENT '缩略图。配合is_image使用',
`is_image` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '是否是图片 1是 0否',
`user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
`user_name` varchar(100) DEFAULT NULL COMMENT '用户名',
`upload_ip` char(15) DEFAULT NULL COMMENT '上传的IP',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`listorder` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
`status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '状态 0:无效 1有效',
PRIMARY KEY (`file_id`),
KEY `tab_name` (`from_table`,`from_id`,`input_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='上传附件';
uploadify 上传不成功时的调试
$(function() {
$("#file_upload").uploadify({
'swf' : '/uploadify/uploadify.swf',
'uploader' : '/uploadify/uploadify.php',
'onUploadSuccess' : function(file, data, response) {
alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
}
});
});
PHP自动将图片转换成BASE64
<?php
function base64_encode_image ($filename='string',$filetype='string') {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
//file_put_contents('b.png',$imgbinary);
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
?>
<img src="<?php echo base64_encode_image ('a.png','png'); ?>"/>
业务
public function getFileList($from_table, $from_id, $input_name = '') {
$where = ['from_table' => $from_table, 'from_id' => $from_id];
if ($input_name) {
$where['input_name'] = $input_name;
}
$list = $this->where($where)->order('listorder asc')->select();
if (empty($input_name)) {
$list = Arr::group($list, 'input_name');
}
return $list;
}
public function removeFile($from_table, $from_id, $input_name = '') {
$where = ['from_table' => $from_table, 'from_id' => $from_id];
if ($input_name) {
$where['input_name'] = $input_name;
}
$this->where($where)->delete();
}
public function getTablesFileList($from_table, $ids, $input_name = '') {
$where = ['from_table' => $from_table, 'from_id' => ['IN', $ids]];
if ($input_name) {
$where['input_name'] = $input_name;
}
$list = $this->where($where)->order('listorder asc')->select();
$list = Arr::group($list, 'from_id', 'input_name');
return $list;
}
/**
* 上传文件逻辑
*
* @param $file
* @param array $params
* @return array
*/
public function upload($file, array $params)
{
$result = ['file_id' => 0, 'file_url' => '', 'file_name' => '', 'status' => 1, 'err_msg' => ''];
try {
//上传文件
$result = $filesystem->writeStream($filePath, $stream);
if ($result) {
//上传成功后,将文件的读写权限修改为公共读
if (empty($params['from_table'])) {
throw new \Exception('缺少参数');
}
$params['save_path'] = $fileUrl;
$params['file_name'] = $file->getClientOriginalName();
$params['file_size'] = $file->getClientSize();
$ext = $file->getClientOriginalExtension();
if (in_array($ext, ['jpg', 'jpeg', 'gif', 'bmp', 'png'])) {
$params['is_image'] = 1;
}
$info = self::store($params);
$result['file_id'] = $info['file_id'];
$result['file_url'] = $fileUrl;
$result['file_name'] = $file->getClientOriginalName();
} else {
throw new \Exception('文件上传失败');
}
} catch (\Exception $e) {
$result['err_msg'] = $e->getMessage();
$result['status'] = 0;
}
return $result;
}