Blogger Information
Blog 28
fans 0
comment 0
visits 15968
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2018-08-28文件操作+遍历目录+上传文件
阿小的博客
Original
757 people have browsed it

实例

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
<style type="text/css">
span{
	font-weight:bold;
}
</style>
</head>
<body>
<fieldset style="width:600px">
<legend>文件上传</legend>
<!-- $_SERVER['PHP_SELF']传输到当前界面 -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
	<input type="file" name="upload" ><br>
	<button type="submit">文件上传</button>
</form>
</fieldset>
<?php 
//print_r($_SERVER);
//print_r($_FILES);
//echo __DIR__;
echo DIRECTORY_SEPARATOR;
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	//检测是否有文件上传
	if (isset($_FILES['upload'])){
		//设置允许上传的文件类型
		$allow = array('image/jpg','image/gif','image/png');
		if(in_array($_FILES['upload']['type'],$allow)){
			//将文件上传到临时目录
			$tmpdir = $_FILES['upload']['tmp_name'];
			$desdir = __DIR__.DIRECTORY_SEPARATOR.$_FILES['upload']['name'];
//			echo $tmpdir,'<br>';
//			echo $desdir,'<br>';
			if(move_uploaded_file($tmpdir,$desdir)){
				echo '<script>alert("上传成功")</script>';
			}else{
				echo '<script>alert("移动失败")</script>';
			}
		}else{
			echo '<script>alert("文件类型不对")</script>';
		}
	}else{
		$res = $_FILES['upload']['error'];
		switch($res){
			//其值为 0,没有错误发生,文件上传成功。
			case 1:
				echo '<script>alert("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值")</script>';
				break;
			case 2:
				echo '<script>alert("上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值")</script>';
				break;
			case 3:
				echo '<script>alert("文件只有部分被上传。")</script>';
				break;
			case 4:
				echo '<script>alert("没有文件被上传。")</script>';
				break;
			case 7:
				echo '<script>alert("文件写入失败。")</script>';
				break;
			case 6:
				echo '<script>alert("找不到临时文件夹。")</script>';
				break;
			default:
				echo '<script>alert("错误!</script>")';
				break;
		}
	}
}else{
	echo '<script>alert("上传方式不对")</script>';
}
?>
<hr>
<h3>遍历目录</h3>
<pre>
<?php 
echo '<span>1.传统的过程函数:opendir() ,readdir(),closedir()</span><br>';
//$dir=__FILE__;常量,获取当前文件路径
echo '当前文件名是',basename(__FILE__),'<br>';//获取当前文件名
echo '当前目录是',dirname(__FILE__),'<br>';	//获取当前文件目录 和__DIR__一样
//__DIR__常量,获取当前文件目录
$dir=opendir(__DIR__) or die("目录打开失败");
while(($file=(readdir($dir))) != false){
	echo nl2br($file."\n");
	
}
closedir($dir);
echo '<hr>';
echo '<span>2.目录扫描器scandir()</span><br>';
$filearr=scandir(__DIR__) or die("目录打开失败");	//返回值是数组
foreach($filearr as $file){
	if($file!='.' && $file!='..'){
		echo nl2br($file."\n");
	}
}


?>
</pre>
</body>
</html>

运行实例 »

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


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