如何封装PHP中的文件上传功能函数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计620个文字,预计阅读时间需要3分钟。
PHP封装PHP文件上传函数示例:
function uploadFile($fileInfo, $uploadPath='./attachment', $flag=true, $allowExt=['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize=2097152) { if ($fileInfo['error']==0) { $arr=explode('.', $fileInfo['name']); // 更多代码... }}
PHP文件上传函数封装
-
<?php -
function uploadFile( $fileInfo, $uploadPath = './attachment', $flag = true, $allowExt = ['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize = 2097152 ) { -
if ( $fileInfo['error'] == 0 ) { -
$arr = explode( '.', $fileInfo['name'] ); -
//explode 分割字符串,以.点分割 -
$ext = end( $arr ); -
//取后缀名 -
$prefix = array_shift( $arr ); -
//取文件名 -
if ( !in_array( $ext, $allowExt ) ) { -
//判断后缀是否存在 -
return $res = '文件类型不合法'; -
} -
if ( $fileInfo['size'] > $maxSize ) { -
//判断文件大小 -
return $res = '文件大小超过限制的最大值'; -
} -
if ( !getimageSize( $fileInfo['tmp_name'] ) ) { -
//检测图片是否合法 -
return $res = '请勿上传非法图片'; -
} -
if ( !is_uploaded_file( $fileInfo['tmp_name'] ) ) { -
//检测上传方式是否为img.558idc.com/uploadfile.php" -
method="post" -
enctype="multipart/form-data" -
> -
<fieldset> -
<legend>文件上传</legend> -
<label for="my_file"></label> -
<input type="file" name="my_file" id="my_file" /> -
<button>上传</button> -
</fieldset> -
</form> -
</body> -
</html>
成功上传文件,并按年/月分类存放
本文共计620个文字,预计阅读时间需要3分钟。
PHP封装PHP文件上传函数示例:
function uploadFile($fileInfo, $uploadPath='./attachment', $flag=true, $allowExt=['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize=2097152) { if ($fileInfo['error']==0) { $arr=explode('.', $fileInfo['name']); // 更多代码... }}
PHP文件上传函数封装
-
<?php -
function uploadFile( $fileInfo, $uploadPath = './attachment', $flag = true, $allowExt = ['jpg', 'png', 'wbmp', 'gif', 'jpeg', 'bmp'], $maxSize = 2097152 ) { -
if ( $fileInfo['error'] == 0 ) { -
$arr = explode( '.', $fileInfo['name'] ); -
//explode 分割字符串,以.点分割 -
$ext = end( $arr ); -
//取后缀名 -
$prefix = array_shift( $arr ); -
//取文件名 -
if ( !in_array( $ext, $allowExt ) ) { -
//判断后缀是否存在 -
return $res = '文件类型不合法'; -
} -
if ( $fileInfo['size'] > $maxSize ) { -
//判断文件大小 -
return $res = '文件大小超过限制的最大值'; -
} -
if ( !getimageSize( $fileInfo['tmp_name'] ) ) { -
//检测图片是否合法 -
return $res = '请勿上传非法图片'; -
} -
if ( !is_uploaded_file( $fileInfo['tmp_name'] ) ) { -
//检测上传方式是否为img.558idc.com/uploadfile.php" -
method="post" -
enctype="multipart/form-data" -
> -
<fieldset> -
<legend>文件上传</legend> -
<label for="my_file"></label> -
<input type="file" name="my_file" id="my_file" /> -
<button>上传</button> -
</fieldset> -
</form> -
</body> -
</html>
成功上传文件,并按年/月分类存放

