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
);
}
}
?>
查看全文
相关阅读:
WPF中的Command事件绑定
WPF的EventAggregator的发布和订阅
IE浏览器如何调试Asp.net的 js代码
MVVM模式用依赖注入的方式配置ViewModel并注册消息
SQL处理数组,字符串转换为数组
C#在函数内部获取函数的参数
JS判断字符串长度(中文长度为2,英文长度为1)
.net一般处理程序(httphandler)实现文件下载功能
SQL分页获取数据
URI编码解码
原文地址:https://www.cnblogs.com/zerogo/p/2209311.html
最新文章
【刷题】面筋-Linux 系统中“|”管道的作用是什么
【刷题】面筋-linux统计文件中字符串次数及文件夹下特定类型文件个数
【学习总结】Linux常用命令小结
【刷题】获取一亿数据获取前100个最大值
霍夫曼编码
类型转换
如何判断你的机器是大端还是小端的?
生产者消费者模式(转)
读写锁机制
C++中的单例模式
热门文章
网络知识点大杂烩
网络I/O中的同步、异步、阻塞和非阻塞概念
Linux下进程通信之管道
关于指针的另一些问题
解决windows10 里vs2017 直接开始执行提示“此任务要求应用程序有提升的权限”1.
VS2017创建类库项目后添加不了WPF资源字典
激活Window和office工具
win10 安装IIS说明操作
C# 在webapi项目中配置Swagger
WPF为stackpanel设置滚动条
Copyright © 2011-2022 走看看