700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > php上传文件测试代码 php 文件上传函数的超详细示例

php上传文件测试代码 php 文件上传函数的超详细示例

时间:2018-08-02 14:45:01

相关推荐

php上传文件测试代码 php 文件上传函数的超详细示例

这篇文章主要为大家详细介绍了php 文件上传函数的超详细示例,具有一定的参考价值,可以用来参考一下。

下面跟随512笔记的小编来举个例子吧。

经测试代码如下:

/**

* 文件上传

*

* 返回的数组索引

* 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 文件数据 索引

* @author 512笔记

*

*/

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;

}

/*** 来自512笔记() ***/

注:关于php 文件上传函数的超详细示例的内容就先介绍到这里,更多相关文章的可以留意512笔记的其他信息。

关键词:文件上传

您可能感兴趣的文章

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。