php 文件上传函数(超详细)
* 文件上传
*
* 返回的数组索引
* mime_type 文件类型
* size 文件大小(单位KB)
* file_path 文件路径
* width 宽度
* height 高度
* 可选值(仅在上传文件是图片且系统开启缩略图时起
/** * 文件上传 * * 返回的数组索引 * 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($attachpathPHP文件上传, 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; } (编辑:成都站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |