Blogger Information
Blog 31
fans 3
comment 1
visits 34404
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php文件上传的实现过程
php学习笔记
Original
770 people have browsed it

php文件上传的实现过程,下面以实例进行演示:

实例

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="542488">
	<fieldset>
		<legend align="center">文件上传</legend>
		<p><strong>选择文件:</strong><input type="file" name="upload" id="upload"></p>
	</fieldset>	
	<p align="center"><button type="submit" name="submit" >上传</button></p>
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    if(isset($_FILES['upload'])){
    	$allow = ['image/jpg','image/jpeg','image/png'];
    	if(in_array($_FILES['upload']['type'],$allow)){
    		if(move_uploaded_file($_FILES['upload']['tmp_name'],"upload/{$_FILES['upload']['name']}")){
    			echo "<script>alert('文件上传成功')</script>";
    		}
    	}else{
    		echo "<script>alert('仅允许上传jpg和png格式的图片')</script>";
    	}
    }

    if($_FILES['upload']['error']>0){
    	echo '上传错误';
    }
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

运行结果:

1.png

2.png

3.png

总结:

$_FILES['file']['name']: 文件原始名称

$_FILES['file']['type']: 文件类型

$_FILES['file']['size']: 上传的文件大小

$_FILES['file']['tmp_name']: 服务器上的临时文件夹

$_FILES['file']['error']: 上传错误代码

$_SERVER['PHP_SELF']:当前php脚本

enctype="multipart/form-data" :允许通过表单上传文件

method:请求类型必须是POST

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post