文件上传与检测
实例
<?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);//关闭目录
运行实例 »
个人总结:
文件的上传与检测有点复杂。一边看一边抄,看来还是要多加练习啊