博客
关于我
php.ini maxfileuploads,细说PHP高洛峰文件上传类源文件
阅读量:793 次
发布时间:2023-02-28

本文共 25428 字,大约阅读时间需要 84 分钟。

以下是兄弟连PHP视频教程中高洛峰老师所教授的文件上传类源代码,记录以备后续参考:

$value){

$key=strtolower($key);

if(!in_array($key, get_class_vars(get_class($this)))){

continue;

}

$this->setOption($key, $value);

}

}

/*function __construct($filepath, $allowtype, $maxsize, $israndname){

$this->filepath=$filepath;

$this->allowtype=$allowtype;

$this->maxsize=$maxsize;

$this->israndname=$israndname;

}*/

private function getError(){

$str="上传文件{$this->originName}时出错:";

switch ($this->errorNum) {

case 4:

$str.="没有文件补上传";

break;

case 3:

$str __("文件只被部分上传");

break;

case 2:

$str __("上传文件超过了html表单中MAX_FILE_SIZE中设定的值");

break;

case 1:

$str __("上传文件超过了php.ini中upload_max_filesize选项的值");

break;

case -1:

$str __("未允许的类型");

break;

case -2:

$str __("文件过大,上传不能超过{$this->maxsize}个字节");

break;

case -3:

$str __("上传失败");

break;

case -4:

$str __("建立存放上传文件目录失败,请重新指定上传目录");

break;

case -5:

$str __("必须指定上传文件的路径");

break;

default:

$str __("未知错误");

break;

}

return $str."

}

private function checkFilePath(){

if(empty($this->filepath)){

$this->setOption('errorNum', -5);

return false;

}

if(!file_exists($this->filepath) || !is_writable($this->filepath)){

if(!@mkdir($this->filepath, 0755)){

$this->setOption('errorNum', -4);

return false;

}

return true;

}

private function checkFileSize(){

if($this->fileSize > $this->maxsize){

$this->setOption('errorNum', -2);

return false;

}else{

return true;

}

private function checkFileType(){

if(in_array(strtolower($this->fileType), $this->allowtype)){

return true;

}else{

$this->setOption('errorNum', -1);

return false;

}

private function setNewFileName(){

if($this->israndname){

$this->setOption('newFileName', $this->proRandName());

}else{

$this->setOption('newFileName', $this->originName);

}

private function proRandName(){

$fileName=date("YmdHis").rand(100,999);

return $fileName.'.'.$this->fileType;

}

private function setOption($key, $value){

$this->$key = $value;

}

//用于上传文件
function uploadFile($fileField){

$return=true;

//检查文件上传路径
if(!$this->checkFilePath()){

$this->errorMess = $this->getError();

return false;

}

$name=$_FILES[$fileField]['name'];

$tmp_name=$_FILES[$fileField]['tmp_name'];

$size=$_FILES[$fileField]['size'];

$error=$_FILES[$fileField]['error'];

//判断是否上传多个文件
if(is_array($name)){

$errors=array();

for($i=0; issetFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){

if(!$this->checkFileSize() || !$this->checkFileType()){

$errors[] = $this->getError();

return false;

}

}else{

$errors[] = $this->getError();

return false;

}

}else{

$errors[] = $this->getError();

return false;

}

if(!$return){

$this->setFiles(); //错误时初始化

}

}

if($return){

$fileNames=array();

for($i=0; issetFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){

$this->setNewFileName();

if(!$this->copyFile()){

$errors = $this->getError();

$return=false;

}else{

$fileNames[] = $this->newFileName;

}

}

}

}else{

if($this->setFiles($name, $tmp_name, $size, $error)){

if($this->checkFileSize() && $this->checkFileType()){

$this->setNewFileName();

if($this->copyFile()){

return true;

}else{

return false;

}

}else{

$return=false;

}

}else{

$return=false;

}

if(!$return){

$this->errorMess = $this->getError();

}

return $return;

}

private function copyFile(){

if(!$this->errorNum){

$filepath=rtrim($this->filepath, '/').'/';

$filepath .= $this->newFileName;

if(@move_uploaded_file($this->tmpFileName, $filepath)){

return true;

}else{

$this->setOption('errorNum', -3);

return false;

}

}else{

return false;

}

}

//设置与$_FILES有关的内容
private function setFiles($name="",$tmp_name="",$size=0,$error=0){

$this->setOption('errorNum', $error);

if($error){

return false;

}

$this->setOption('originName', $name);

$this->setOption('tmpFileName', $tmp_name);

$arrStr = explode('.', $name);

$this->setOption('fileType', strtolower($arrStr[count($arrStr)-1]));

$this->setOption('fileSize', $size);

return true;

}

//用于获取上传后文件的文件名
function getNewFileName(){

return $this->newFileName;

}

//上传如果失败,则调用这个方法查看错误报告
function getErrorMsg(){

return $this->errorMess;

}

}

//可在提交表单时将action设置为另一个单独的php文件,并通过一些值的设定,如上传路径,是否使用随机文件名,允许格式来初始化类中所定义的私有变量。以下代码中的pic为上传文件表单input中的name值。

require("FileUpload.class.php");

$up=new UploadFile(array('filepath' => './upload/', 'MAXSIZE' => 2000000, 'isRandName' => true, 'allowtype' => array('jpg', 'gif')));

if($up->uploadFile('pic')){

echo $up->getNewFileName();

}else{

echo $up->getErrorMsg();

}

转载地址:http://futfk.baihongyu.com/

你可能感兴趣的文章
Oracle中常用的语句
查看>>
oracle中新建用户和赋予权限
查看>>
Oracle中的rownum 和rowid的用法和区别
查看>>
oracle中的大小写、字符、dual、数字、处理、日期、函数、显/隐式、时间、条件表达式case、decode、to_date、to_char、sysdate
查看>>
oracle典型安装失败,安装oracle 10失败
查看>>
Oracle分析函数之LEAD和LAG
查看>>
Oracle监听配置、数据库实例配置等
查看>>
oracle系统 介绍,ORACLE数据库管理系统介绍
查看>>
oracle获取数据库表、字段、注释、约束等
查看>>
Oracle计划将ZGC项目提交给OpenJDK
查看>>
Oracle闪回技术(Flashback)
查看>>
oracle零碎要点---ip地址问题,服务问题,系统默认密码问题
查看>>
oracle零碎要点---oracle em的web访问地址忘了
查看>>
Oracle零碎要点---多表联合查询,收集数据库基本资料
查看>>
Oracle静默安装
查看>>
Oracle面试题:Oracle中truncate和delete的区别
查看>>
Oracle:ORA-00911: 无效字符
查看>>
TCP基本入门-简单认识一下什么是TCP
查看>>
Orcale表被锁
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>