Home > Backend Development > PHP Tutorial > PHP single file upload principle and upload function encapsulation_PHP tutorial

PHP single file upload principle and upload function encapsulation_PHP tutorial

WBOY
Release: 2016-07-13 09:54:52
Original
791 people have browsed it

PHP single file upload principle and packaging of upload function

<!--?php
//单文件上传函数的封装
//文件上传原理:将客户端的文件上传到服务器端,再将服务器端的临时文件移动到指定目录即可。
//文件的方向:客户端——-->服务器(临时文件)&mdash;&mdash;>指定目录,当文件进入服务器时它就是临时文件了,这时操作中要用临时文件的名称tmp_name。
//在客户端设置上传文件的限制(文件类型和大小)是不安全的,因为客户能通过源代码修改限制,所以在服务端这里设置限制。
//设置编码为UTF-8,以避免中文乱码 
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
//通过$_FILES接收上传文件的信息
$fileInfo = $_FILES[&#39;myFile&#39;];
function uploadFile($fileInfo,$uploadPath=&#39;uploads&#39;,$flag=true,$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;gif&#39;),$maxSize = 2097152){
//判断错误号,只有为0或者是UPLOAD_ERR_OK,没有错误发生,上传成功
	if($fileInfo[&#39;error&#39;]>0){
		//注意!错误信息没有5
		switch($fileInfo[&#39;error&#39;]){
			case 1:
				$mes= &#39;上传文件超过了PHP配置文件中upload_max_filesize选项的值&#39;;
				break;
			case 2:
				$mes= &#39;超过了HTML表单MAX_FILE_SIZE限制的大小&#39;;
				break;
			case 3:
				$mes= &#39;文件部分被上传&#39;;
				break;
			case 4:
				$mes= &#39;没有选择上传文件&#39;;
				break;
			case 6:
				$mes= &#39;没有找到临时目录&#39;;
				break;
			case 7:
				$mes= &#39;文件写入失败&#39;;
				break;
			case 8:
				$mes= &#39;上传的文件被PHP扩展程序中断&#39;;
				break;
				
		}	
		exit($mes);
	}
	$ext=pathinfo($fileInfo[&#39;name&#39;],PATHINFO_EXTENSION);
	//$allowExt=array(&#39;jpeg&#39;,&#39;jpg&#39;,&#39;png&#39;,&#39;gif&#39;);
	
	//检测上传文件的类型
	if(in_array($ext,$allowExt)){
		exit(&#39;非法文件类型&#39;);	
	}
	
	//检测上传文的件大小是否符合规范
	//$maxSize = 2097152;//2M
	if($fileInfo[&#39;size&#39;]>$maxSize){
		exit(&#39;上传文件过大&#39;);	
	}
	
	//检测图片是否为真实的图片类型
	//$flag=true;
	if($flag){
		if(!getimagesize($fileInfo[&#39;tmp_name&#39;])){
			exit(&#39;不是真实的图片类型&#39;);	
		}	
	}
	
	//检测是否是通过HTTP POST方式上传上来
	if(!is_uploaded_file($fileInfo[&#39;tmp_name&#39;])){
		exit(&#39;文件不是通过HTTP POST方式上传上来的&#39;);	
	}
	
	//$uploadPath=&#39;uploads&#39;;
	//如果没有这个文件夹,那么就创建一个
	if(!file_exists($uploadPath)){
		mkdir( $uploadPath, 0777, true);
		chmod( $uploadPath, 0777 );
	}
	//新文件名唯一
	$uniName = md5 ( uniqid( microtime(true),true) ).&#39;.&#39;.$ext;
	$destination = $uploadPath.&#39;/&#39;.$uniName;
	//@符号是为了不让客户看到错误信息
	if(! @move_uploaded_file($fileInfo[&#39;tmp_name&#39;], $destination )){
		exit(&#39;文件移动失败&#39;);	
	}
	
	//echo &#39;文件上传成功&#39;;
	//return array(
	//	&#39;newName&#39;=>$destination,
	//	&#39;size&#39;=>$fileInfo[&#39;size&#39;],
	//	&#39;type&#39;=>$fileInfo[&#39;type&#39;]
	//);
	return $destination;
}
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/995650.htmlTechArticlePHP single file upload principle and the encapsulation server (temporary file) of the upload function specifies the directory. When the file enters the server, it It is a temporary file. In this case, the name of the temporary file must be used during the operation...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template