Blogger Information
Blog 27
fans 0
comment 0
visits 17753
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件与目录操作-8.28
Yyk.的博客
Original
527 people have browsed it

文件上传与检测

实例

<?php 
	//检测请求类型是否POST,如果不是应该提示用户类型不对
	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 '<p>错误原因是:<strong>';

			switch ($_FILES['upload']['error']) {
				case 1:
					echo '文件超过了php.ini配置中设置的大小';
					break;
				case 2:
					echo '文件超过了表单中常量设置的大小';
					break;
				
			}

			echo '</strong></p>';
			if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {
				unlink($_FILES['upload']['tmp_name']);
			}
		}
	} else {
		echo '1';
	}
?>

运行实例 »

目录遍历

实例

$dir = opendir('../0827') or die('打开失败'); //打开目录
while (false != ($file = readdir($dir))) {//读取目录


	if ($file != "." && $file != "..") {      
			print $file."<br>";
     }
}
closedir($dir);//关闭目录

运行实例 »

个人总结:

        文件的上传与检测有点复杂。一边看一边抄,看来还是要多加练习啊



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