zoukankan
html css js c++ java
PHP文件类上传,样图,文件夹内容浏览删除
<?
php
/*
* 文件操作类,create by linyupark,
*/
class
File
{
protected
$path
;
//
根目录例如 ./upload
function
__construct(
$path
)
{
if
(
!
is_dir
(
$path
))
mkdir
(
$path
,
0777
);
$this
->
path
=
$path
;
}
/*
* # 文件上传函数
* @ input_name 为表单中type为file的name值
* @ ext_arr 为允许使用的扩展名数组,请用小写,例array('jpg','gif')
* @ max_size 文件大小,单位b,上限请修改php.ini中的post上限和upload上限
* @ rand 是否把上传的文件改为随机数,避免相同文件名上传
* @ sample 如果文件为图象可以给该参数指定宽或高(x,y),比如array(100,'auto')
*/
public
function
upload(
$input_name
,
$ext_arr
,
$max_size
,
$rand
=
0
,
$sample
=
null
)
{
//
基本错误
if
(
$_FILES
[
$input_name
][
'
error
'
]
>
0
)
{
switch
(
$_FILES
[
$input_name
][
'
error
'
])
{
case
3
:
return
"
只上传了部分文件
"
;
break
;
case
4
:
return
"
没有文件上传
"
;
break
;
default
:
return
"
文件大小超标
"
;
}
exit
();
}
//
文件大小
if
(
$_FILES
[
$input_name
][
'
size
'
]
>
$max_size
)
{
return
"
文件大小超标
"
;
exit
();
}
//
文件类型
$type
=
array_shift
(
explode
(
"
/
"
,
$_FILES
[
$input_name
][
'
type
'
]));
if
(
!
is_dir
(
"
{$this->path}/{$type}
"
))
mkdir
(
"
{$this->path}/{$type}
"
);
//
扩展名
$file_ext
=
strtolower
(
array_pop
(
explode
(
"
.
"
,
$_FILES
[
$input_name
][
'
name
'
])));
if
(
!
in_array
(
$file_ext
,
$ext_arr
))
{
return
"
文件扩展名不符合要求
"
;
exit
();
}
//
文件名
if
(
$rand
==
1
)
$file_name
=
time
()
.
rand
(
1000
,
9999
)
.
"
.
"
.
$file_ext
;
else
$file_name
=
strtolower
(
$_FILES
[
$input_name
][
'
name
'
]);
//
临时文件转移
if
(
is_uploaded_file
(
$_FILES
[
$input_name
][
'
tmp_name
'
]))
{
if
(
!
move_uploaded_file
(
$_FILES
[
$input_name
][
'
tmp_name
'
]
,
"
{$this->path}/{$type}/{$file_name}
"
))
{
return
"
上传的文件移动过程中失败
"
;
exit
();
}
}
else
{
return
"
无法找到上传的临时文件
"
;
exit
();
}
//
样图
if
(
$type
==
"
image
"
&&
$sample
!=
null
)
{
list
(
$width
,
$height
)
=
getimagesize
(
"
{$this->path}/{$type}/{$file_name}
"
);
list
(
$x
,
$y
)
=
$sample
;
if
(
$x
!=
"
auto
"
)
$s_width
=
$x
;
else
$s_width
=
(
$y
/
$height
)
*
$width
;
if
(
$y
!=
"
auto
"
)
$s_height
=
$y
;
else
$s_height
=
(
$x
/
$width
)
*
$height
;
$dst_img
=
imagecreatetruecolor(
$s_width
,
$s_height
);
switch
(
$file_ext
)
{
case
'
gif
'
:
$image
=
imagecreatefromgif(
"
{$this->path}/{$type}/{$file_name}
"
);
break
;
case
'
png
'
:
$image
=
imagecreatefrompng(
"
{$this->path}/{$type}/{$file_name}
"
);
break
;
case
'
bmp
'
:
$image
=
imagecreatefromwbmp(
"
{$this->path}/{$type}/{$file_name}
"
);
break
;
default
:
$image
=
imagecreatefromjpeg(
"
{$this->path}/{$type}/{$file_name}
"
);
}
imagecopyresampled(
$dst_img
,
$image
,
0
,
0
,
0
,
0
,
$s_width
,
$s_height
,
$width
,
$height
);
//
统一输出为jpg
if
(
!
is_dir
(
"
{$this->path}/sample
"
))
mkdir
(
"
{$this->path}/sample
"
);
$sample_name
=
array_shift
(
explode
(
"
.
"
,
$file_name
))
.
"
.jpg
"
;
imagejpeg(
$dst_img
,
"
{$this->path}/sample/{$sample_name}
"
,
100
);
}
return
"
上传任务完成
"
;
}
/*
* # 文件浏览功能函数
* @ url 执行函数的路径
* @ dir_arg 切换目录的参数变量
* @ value 切换目录的值,目录名不能为中文
* @ del_arg 删除参数变量
*/
public
function
browser(
$url
,
$dir_arg
,
$del_arg
=
null
)
{
//
是否有参数值
if
(
!
isset
(
$_GET
[
$dir_arg
]))
$value
=
$this
->
path;
else
$value
=
$_GET
[
$dir_arg
];
//
有删除操作
if
(
isset
(
$_GET
[
$del_arg
]))
{
if
(
is_dir
(
$_GET
[
$del_arg
]))
$this
->
rmdir
(
$_GET
[
$del_arg
]);
else
unlink
(
$_GET
[
$del_arg
]);
}
$dir
=
dir
(
$value
);
$pre_value
=
$this
->
path;
//
初始父目录
$str
=
"
<dl>
"
;
//
是否有上一级目录
if
(
$value
!=
$this
->
path)
{
$pre_value
=
explode
(
"
/
"
,
$value
);
array_pop
(
$pre_value
);
$pre_value
=
implode
(
"
/
"
,
$pre_value
);
$str
.=
"
<dt><a href='{$url}?{$dir_arg}={$pre_value}'>父目录</a></dt>
"
;
}
while
(
false
!==
(
$unit
=
$dir
->
read()))
{
if
(
$unit
!=
"
.
"
&&
$unit
!=
"
..
"
)
{
//
是否为目录
if
(
is_dir
(
"
{$value}/{$unit}
"
))
{
if
(
$del_arg
!=
null
)
$str
.=
"
<dt><a href='{$url}?{$dir_arg}={$value}/{$unit}'>{$unit}/</a>
<a href='{$url}?{$dir_arg}={$pre_value}&{$del_arg}={$value}/{$unit}'>删除</a></dt>
"
;
else
$str
.=
"
<dt><a href='{$url}?{$dir_arg}={$value}/{$unit}'>{$unit}/</a></dt>
"
;
}
else
{
$file
=
substr
(
$value
,
1
)
.
"
/
"
.
$unit
;
if
(
$del_arg
!=
null
)
$str
.=
"
<dd><a href='{$file}'>{$unit}</a>
<a href='{$url}?{$dir_arg}={$value}&{$del_arg}={$value}/{$unit}'>删除</a></dd>
"
;
else
$str
.=
"
<dd><a href='{$file}'>{$unit}</a></dd>
"
;
}
}
}
$dir
->
close();
$str
.=
"
</dl>
"
;
return
$str
;
}
/*
* 内部函数,删除目录以及里面的所有文件
*/
protected
function
rmdir
(
$path
)
{
$dir
=
dir
(
$path
);
while
(
false
!==
(
$unit
=
$dir
->
read()))
{
if
(
$unit
!=
"
.
"
&&
$unit
!=
"
..
"
)
{
if
(
is_dir
(
$path
.
"
/
"
.
$unit
))
{
$this
->
rmdir
(
$path
.
"
/
"
.
$unit
);
}
else
unlink
(
$path
.
"
/
"
.
$unit
);
}
}
$dir
->
close();
rmdir
(
$path
);
}
}
?>
查看全文
相关阅读:
Atitit 图像处理30大经典算法attilax总结
Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结
Atitit rgb yuv hsv HSL 模式和 HSV(HSB) 图像色彩空间的区别
Atitit 从 RGB 到 HSL 或 HSV 的转换
Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理
Atitit 修改密码的功能流程设计 attilax总结
atitit 点播系统 概览 v2 qb1.docx
Atitit dsl exer v3 qb3 新特性
atitit.TokenService v3 qb1 token服务模块的设计 新特性.docx
Atitit 异常机制与异常处理的原理与概论
原文地址:https://www.cnblogs.com/zerogo/p/2209311.html
最新文章
Atitit DbServiceV4qb9 数据库查询类库v4 新特性
Atitit 基于图片图像 与文档混合文件夹的分类
Atitit 图像处理之理解卷积attilax总结
Atitit 图像处理—图像形态学(膨胀与腐蚀)
Atitit 游戏的通常流程 attilax 总结 基于cocos2d api
Atitit数据库层次架构表与知识点 attilax 总结
Atitti css transition Animation differ区别
JS设置cookie、读取cookie、删除cookie
Atitit 代理与分销系统(1) 子代理 充值总额功能设计概览 sum() groubpy subagt
Atitit截屏功能的设计解决方案
热门文章
Atitit 图像处理底色变红的解决
Atitit 混合叠加俩张图片的处理 图像处理解决方案 javafx blend
Atitit 桌面软件跨平台gui解决方案 javafx webview
Atitit blend mode COLOR_DODGE 混合模式 “颜色减淡”模式
Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结
Atitit vod ver 12 new feature v12 pb2 影吧 视频 电影 点播 播放系统v12新特性
Atitit 全屏模式的cs桌面客户端软件gui h5解决方案 Kiosk模式
atitit.vod search doc.doc 点播系统搜索功能设计文档
Atitit vod click event design flow 视频点播系统点击事件文档
Atiti attilax主要成果与解决方案与案例rsm版 v2
Copyright © 2011-2022 走看看