Blogger Information
Blog 34
fans 0
comment 0
visits 26580
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
遍历目录,上传文件实例
罗盼的博客
Original
555 people have browsed it

实例

<?php
//遍历目录
function my_dir($dir) {
    $files = array();
    if(@$handle = opendir($dir)) { //注意这里要加一个@,不然会有warning错误提示:)
        while(($file = readdir($handle)) !== false) {
            if($file != ".." && $file != ".") { //排除根目录;
                if(is_dir($dir."/".$file)) { //如果是子文件夹,就进行递归
                    $files[$file] = my_dir($dir."/".$file);
                } else { //不然就将文件的名字存入数组;
                    $files[] = $file;
                }
 
            }
        }
        closedir($handle);
        return $files;
    }
}
echo "<pre>";
print_r(my_dir("../bloger/shuzu"));
echo "</pre>";
?>
运行实例 »

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

实例

//文件上传

<form method="post" action="<?php htmlspecialchars($_SERVER['PHP_SELF'])?>" enctype="multipart/form-data"  >

名字 <input type="text" name="nickname" value=" " type=""/>
<br/>
密码<input type="password" name="password" value="" />
<br/>
图像 <input class="form-control" name="head" type="file" />

<input type="submit" value="提交"/>
</form>
<?php
header("content-type:text/html;charset=utf-8");
//echo htmlspecialchars($_SERVER['PHP_SELF']);
//echo '<pre>';
//print_r($_SERVER);
date_default_timezone_set('PRC');//设置中国时区
$images_type = array('image/jpeg','image/png','image/jpg','image/gif');//这个可以在前端进行筛选,给file加一个accept="image/jpeg,image/gif,image/png"
if($_SERVER['REQUEST_METHOD'] == 'POST'){
  //print_r($_FILES['head']);
      if(isset($_FILES['head'])){
        $error=$_FILES['head']['error'];//获取错误类型
            if($error ==0){
                     if(in_array($_FILES['head']['type'],$images_type)){
                            if($_FILES['head']['name']){        
                               $old_name = $_FILES['head']['name']; //取出老图片的名字
                               $old_name = explode('.',$old_name);//分割老图片后缀名
                               $old_name = end($old_name);//取出后缀名
                               $new_name = rand(10000,99999);//取随机数
                               $date_name = date('YmdHis',time());//取时间
                               $tem =$_FILES['head']['tmp_name'];//取出老图片的位置
                               $update = 'img/'.$date_name.$new_name.'.'.$old_name;//给新图片取名字加上老图片后缀名                     
                                   if(move_uploaded_file($tem,$update)){
                                    echo '上传图片成功!';
                                   }//把老图片移动到服务器并取新名字                           
                            }     
                      } else{echo '<script>alert(\'图片类型错误!\')</script>';} 
           }else{
                    switch($error){
                        case 1:
                           echo '文件超过php.ini配置大小!';
                           break;
                        case 2:
                           echo '文件超过表单设置大小!';
                           break;
                        case 3:
                           echo '文件部分上传!';
                           break;
                        case 4:
                           echo '没有文件上传!';
                           break;
                        default:
                           echo '未知错误类型!';
                    }
            
           }           
      }

}

?>

运行实例 »

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


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