search
Homephp教程php手册PHP单文件上传原理及上传函数的封装

<!--?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;
}
?>



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.