php中file对象实例,php文件上传类和例子

/*

* 文件上传类 return code 101 :文件大小超过代码设置最大值 102 :文件超过php.ini中设置上传的最大值,php获取不到$_FILE数组 103:文件格式不在允许上传的范围之内! 104 :上传失败:您上传的文件格式不正确! 105 :move_uploaded_file函数执行失败,可能是权限问题 true:上传成功

*/

class upload

{

/**

* 文件大小

*

* @var integer

*/

protected $limitSize;

/**

* 文件名字

*

* @var string

*/

protected $fileName;

/**

* 文件类型

*

* @var string

*/

protected $limitType;

/**

* 构造函数

*

* @access public

* @return boolean

*/

public function __construct()

{

$this->limitSize = 1048576000; // 默认文件大小 1000M

return true;

}

/**

* 初始化运行环境

*

* @param string $file

* @return boolean

*/

protected function parseInit($file)

{

$this->fileName = $file;

if ($this->fileName ['size'] > $this->limitSize)

{

// return '上传文件超出最大限制15M';

return 101;

exit ();

}

if ($this->limitType)

{

return $this->parseMimetype ( $file );

}

return true;

}

/**

* 设置上传文件的大小限制.

*

* @param integer $size

* @return unkonow

*/

public function setLimitSize($size)

{

if ($size)

{

$this->limitSize = $size;

}

return $this;

}

/**

* 设置上传文件允许的格式

*

* @param string $type

* @return unkonow

*/

public function setLimitType($type)

{

if (! $type || ! is_array ( $type ))

{

return false;

}

$this->limitType = $type;

return $this;

}

/**

* 验证上传文件的格式

*

* @return boolean

*/

protected function parseMimetype()

{

// 上传文件允许的格式

$mimeType = array (

'jpg' => 'image/jpeg',

'gif' => 'image/gif',

'png' => 'image/png',

'bmp' => 'image/bmp',

'html' => 'text/html',

'css' => 'text/css',

'wbmp' => 'image/vnd.wap.wbmp',

'js' => 'application/x-javascript',

'swf' => 'application/x-shockwave-flash',

'xml' => 'application/xhtml+xml',

'php' => 'application/x-httpd-php',

'txt' => 'text/plain',

'wma' => 'audio/x-ms-wma',

'mp3' => 'audio/mpeg',

'excel' => 'application/vnd.ms-excel',

'excel2' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

'zip' => 'application/zip',

'zip1' => 'application/octet-stream',

'rar' => 'application/x-rar-compressed',

'flv' => 'flv-application/octet-stream'

)

;

// 判断limitType是否在允许上传文件格式列表之内

$mimeTypeKey = array_keys ( $mimeType );

foreach ( $this->limitType as $type )

{

if (! in_array ( $type, $mimeTypeKey ))

{

// return '文件格式不在允许上传的范围之内!';

return 103;

exit ();

}

}

$allowTypeArray = array ();

foreach ( $this->limitType as $type )

{

$allowTypeArray [] = $mimeType [$type];

}

if (! in_array ( $this->fileName ['type'], $allowTypeArray ))

{

// return '上传失败:您上传的文件格式不正确!';

return 104;

exit ();

}

return true;

}

/**

* 上传文件

*

* @param string $fileUpload

* 文件名字

* @param string $fileName

* 上传后的目标文件名

* @return boolean

*/

public function upload($fileUpload, $fileName)

{

// 参数分析

if (! is_array ( $fileUpload ) || empty ( $fileName ))

{

// return '获取不到$_FILE信息';

return 102;

}

$rs = $this->parseInit ( $fileUpload );

if ($rs === TRUE)

{

if (! move_uploaded_file ( $this->fileName ['tmp_name'], $fileName ))

{

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

return 105;

}

return true;

}

else

{

return $rs;

}

}

}

```

再来一个例子

```

$uploadObj = new upload();

$fileTmpNAME = 'uploadfile/report/'.date('Y-m-d').'/'.md5(time().$_FILES['file']['name']).'.'.pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);//上传之后文件路径

$pathInfo = pathinfo($fileTmpNAME);

mkpath($pathInfo['dirname']);//创建目录

$uploadObj->setLimitSize(1024*1024*15);//设置文件大小

$result = $uploadObj->setLimitType(array('excel','excel2'))->upload($_FILES['file'], $fileTmpNAME );//设置类型,并上传

if($result === TRUE)

{

//上传成功

}

else

{

//上传失败,可以根据返回code提示信息

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值