php file 函数,php 文件上传函数(超详细)

/**

* 文件上传

*

* 返回的数组索引

* mime_type 文件类型

* size      文件大小(单位KB)

* file_path 文件路径

* width     宽度

* height    高度

* 可选值(仅在上传文件是图片且系统开启缩略图时起作用)

* thum_file   缩略图的路径

* thum_width  缩略图宽度

* thum_height 缩略图高度

* thum_size   缩略图大小(单位KB)

*

* @param string $fileName 文件名

* @param string $errorNum 错误码:$_FILES['error']

* @param string $tmpFile 上传后的临时文件

* @param string $fileSize 文件大小 KB

* @param array $type 允许上传的文件类型

* @param boolean $isIcon 是否为上传头像

* @param boolean $is_thumbnail 是否生成缩略图

* @return array 文件数据 索引

*

*/

function upload($fileName, $errorNum, $tmpFile, $fileSize, $type, $isIcon = false, $is_thumbnail = true) {

if ($errorNum == 1) {

return '100'; //文件大小超过系统限制

} elseif ($errorNum > 1) {

return '101'; //上传文件失败

}

$extension = getFileSuffix($fileName);

if (!in_array($extension, $type)) {

return '102'; //错误的文件类型

}

if ($fileSize > Option::UPLOADFILE_MAXSIZE) {

return '103'; //文件大小超出emlog的限制

}

$file_info = array();

$file_info['file_name'] = $fileName;

$file_info['mime_type'] = get_mimetype($extension);

$file_info['size'] = $fileSize;

$file_info['width'] = 0;

$file_info['height'] = 0;

$uppath = Option::UPLOADFILE_PATH . gmdate('Ym') . '/';

$fname = substr(md5($fileName), 0, 4) . time() . '.' . $extension;

$attachpath = $uppath . $fname;

$file_info['file_path'] = $attachpath;

if (!is_dir(Option::UPLOADFILE_PATH)) {

@umask(0);

$ret = @mkdir(Option::UPLOADFILE_PATH, 0777);

if ($ret === false) {

return '104'; //创建文件上传目录失败

}

}

if (!is_dir($uppath)) {

@umask(0);

$ret = @mkdir($uppath, 0777);

if ($ret === false) {

return '105'; //上传失败。文件上传目录(content/uploadfile)不可写

}

}

doAction('attach_upload', $tmpFile);

// 生成缩略图

$thum = $uppath . 'thum-' . $fname;

if ($is_thumbnail) {

if ($isIcon && resizeImage($tmpFile, $thum, Option::ICON_MAX_W, Option::ICON_MAX_H)) {

$file_info['thum_file'] = $thum;

$file_info['thum_size'] = filesize($thum);

$size = getimagesize($thum);

if ($size) {

$file_info['thum_width'] = $size[0];

$file_info['thum_height'] = $size[1];

}

resizeImage($tmpFile, $uppath . 'thum52-' . $fname, 52, 52);

} elseif (resizeImage($tmpFile, $thum, Option::IMG_MAX_W, Option::IMG_MAX_H)) {

$file_info['thum_file'] = $thum;

$file_info['thum_size'] = filesize($thum);

$size = getimagesize($thum);

if ($size) {

$file_info['thum_width'] = $size[0];

$file_info['thum_height'] = $size[1];

}

}

}

if (@is_uploaded_file($tmpFile)) {

if (@!move_uploaded_file($tmpFile, $attachpath)) {

@unlink($tmpFile);

return '105'; //上传失败。文件上传目录(content/uploadfile)不可写

}

@chmod($attachpath, 0777);

}

// 如果附件是图片需要提取宽高

if (in_array($file_info['mime_type'], array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'))) {

$size = getimagesize($file_info['file_path']);

if ($size) {

$file_info['width'] = $size[0];

$file_info['height'] = $size[1];

}

}

return $file_info;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值