php文件操作

  1. 文件操作,
  2. 目录操作(文件夹操作)

1.文件的读取

$res = file_get_contents('php3.txt');  //读  一般配合 file_put_contents 写使用

// $res =file_get_contents('http://www.baidu.com');

2.文件的写入

$a = file_get_contents('http://www.baidu.com'); //或者其他文件
$res =file_put_contents('php3.txt','写入方式二 常用');  //从头写入,覆盖  也可以是上面的$a
if($res){
    echo '写入成功';
}else{
    echo '写入失败';
}

3.文件的删除

// $res = unlink('6.txt');
// if($res){
//     echo '删除成功';
// }else{
//     echo '删除失败';
// }

php文件夹操作

//创建文件夹📂
// $rem = mkdir('test1');
// if($res){
//     echo '创建成功';
// }else{
//     echo '创建失败';
// }

读取文件夹📂

$xx = scandir('test'); //读取文件夹,以数组形式返回
foreach ($xx as $key => $value) {
    // $xx[$key] = iconv('gbk',"utf-8",$value); //如果中文文件夹乱码加上这个就好了
    $url = "test/".$value;
    if(is_file($url)){
        echo "<font color='red'>".$value.'</font><br>';
    }else{
        echo "<font color='green'>".$value.'</font><br>';
    }
}
// print_r ($xx);

删除文件夹

// $res = rmdir('test1');  //这个只能删除空文件夹


//删除文件夹及内部文件
function removeDir($path){
    $arr = scandir($path);
//    print_r($arr);die;
    if(count($arr)>2){
        //目录不为空
        for($i=2;$i<count($arr);$i++){
             $url = $path.'/'.$arr[$i];
            //$arr[$i] = 1.txt  test1
             if(is_dir($url)){
                 //为目录. 
                 removeDir($url);
             }else{
                 //是文件
                unlink($url);
             }
        }
    }
    $res = rmdir($path);
    return $res;
}
// removeDir($path)

获取文件的最后创建时间

//$res = filectime('1.txt')  //返回时间戳
//echo date('Y-m-d H:i:s',$res)

//file_exists: 判断文件是否存在;
if(!file_exists('test')){
    mkdir('test');
}

//判断是否可读
is_readable(filename);
//判断是否可写
is_writable(filename);

修改目录权限

chmod('test', 0755); //7=1+4+2   5=1+4  
// 1.执行权限(1)  2.读取(4) 3.可写(2)
//如果是手动创建的是不能用代码来更改权限的

页面跳转

header('Location:file.php') //跳转显示file.php文件