hnr / helper
hnrHelper 的主要目标是特别为了安全原因,在 yii 网站目录外上传和检索上传的文件(图像或文档)。
dev-master
2016-03-17 08:31 UTC
Requires
- yiisoft/yii2-imagine: ~2.0.0
This package is not auto-updated.
Last update: 2024-09-20 18:39:00 UTC
README
文件上传器
其目的是将上传的文件存储在 Yii 网络文件夹之外,然后人们可以通过输入请求的文件 id 和名称通过 URL 访问它们。
上传文件的详细信息存储在数据库中,包含以下信息:
- id
- filename
- filename_real
- size
- content_type
- created_at
- updated_at
- 附加 id 用于参考,例如:id_publikasi, id_member, id_blog 等
文件迁移以创建表
$this->createTable('t_media_file', [
'id' => $this->primaryKey(),
'id_member' => $this->integer()->notNull(),
'album_type' => $this->smallInteger(1), //PROFIL,OTHER
'is_main' => $this->smallInteger(1)->defaultValue(0),
'filename' => $this->string(230), // image or cover photo
'filename_real' => $this->string(250),
'size' => $this->integer()->defaultValue(0),
'content_type' => $this->string(75),
'created_at' => 'integer' ,
'updated_at' => 'integer' ,
]);
例如,上传文件放置在 D:/UPLOAD(如果使用 Windows),或者 Yii 网络文件夹之外的一 _FILES
脚本位于 C:/htdocs/app1/backend/web
URL https:///app1
图片文件的 URL:https:///app1/media/img/9/200/400/1/namafile.jpg
PDF 文件的 URL:https:///app1/media/doc/19/namadoc.pdf 对于 PDF 的情况,创建控制器
- 控制器名称:MediaController
- 之后会有 action:actionDoc($id,$name),它接收参数 id:19,数据库中文件名称:namadoc.pdf 目的是验证记录 id 19 的名称是否与 namadoc.pdf 相同,以确保没有人输入 id 20,21,等等
URL 映射示例
'rules' => array(
'media/img/<id:\d+>/<w>/<h>/<crop>/<nama>' => 'media/img',
'media/doc/<id:\d+>/<nama>' => 'media/doc',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>/<name>' => '<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+>' => '<controller>/<action>',
https:///app1/media/doc?id=19&name=namadoc.pdf
$get=\yii::$app->request->get(); $get['id'] $get['name']
最好将文件系统中的文件重命名,移除空格、逗号、括号等,用 _ 或 - 替换