PHP文件系统处理

<html>
<head>
    <title>index</title>
</head>
<body>
<p>PHP文件系统处理</p>

<?php
//Windows系统下获取文件类型
echo "C:\\windows的类型是:" . filetype('c:\windows') . '<br/>';
echo "C:\\windows\\regedit.exe的类型是:" . filetype('c:\windows\regedit.exe') . '<br/>';

//使用stat()函数获得指定文件名参数目标文件基本属性
$stt = stat("D:\\test.txt");
print_r($stt);
echo "<br>";
$lstt = lstat("D:\\test.txt");
print_r($lstt);
echo "<br>";
?>

<?php
//自定义函数通过整合系统函数单独输出了test.txt文件相关属性
function checkFile($fileName)
{
    if (!file_exists($fileName)) {
        exit("文件不存在!<br/>");
    }
    echo '类型' . filetype($fileName) . "<br/>";
    echo '大小' . filesize($fileName) . "bit <br/>";
    echo '类型' . is_executable($fileName) ? "可执行<br>" : "不可执行" . "<br/>";
    echo '可读' . is_readable($fileName) . "<br/>";
    echo '可写' . is_writable($fileName) . "<br/>";

}

//调用函数
checkFile("D:\\test.txt");
echo fileowner("D:\\test.txt") . '<br/>';
echo filegroup("D:\\test.txt") . '<br/>';

?>

<?php
$url = "D:\\test.txt";

$p = pathinfo($url);
print_r($p);
echo '<br>';
echo basename($url, ".txt") . "<br>";   //不带有文件拓展名

//0777权限为:除访问者不具备权限,其他所有用户、用户组均可操作
$change = chmod('$url', 777);
if ($change) {
    echo '改变权限成功';
} else {
    echo '失败';
}

?>


<?php
echo "<br/>";
//指定读取字节数并赋值到一个变量输出
$url = 'D:\\test.txt';
$read = fopen($url, 'r') or die('打开失败。'); //只读方式打开
$text = fread($read, 10);  //读取10个字节并赋值
fclose($read);
echo $text . '<br/>';

echo "<br/>";
echo "<br/>";
//一次性读取所有内容并赋值到一个变量中,每次读取指定字节,循环输出
$url = 'D:\\test.txt';
$read = fopen($url, 'r') or die('打开失败。'); //只读方式打开
$content = "";
while (!feof($read)) {
    $content .= fread($read, 1);
}
fclose($read);
echo $content;
echo '<br>';
?>

<p>PHP写入文件内容</p>
<form id="form1" name="form1" method="post" action="">
    <input type="text" name="input" id="textfield"/>
    <input type="submit" name="button" id="button" value="写入">
</form>
<?php
$post = $_POST['input'];
$flink = fopen("D:\\test.txt", "a+");
if ($_POST['input']) {
    fwrite($flink, $post);
}

$content = fread($flink, 9999);
fclose($flink);
echo $content;
?>

<p>PHP删除文件</p>
<?php
//删除文件
function delete($fileName)
{
    if (is_file($fileName)) {
        if (unlink($fileName)) {
            echo '删除' . $fileName . '成功';
        } else {
            echo '删除' . $fileName . '失败';
        }
    } else {
        echo $fileName . '不是一个文件或不存在';
    }
}

delete("D:\\dd.txt");
?>

<?php
$url = 'https://www.sohu.com/c/8/1461?spm=smpc.news-home.top-subnav.3.1626963925184e2sscIt';
$furl = fopen($url, 'r');
if ($furl) {
    $content = "";
    while (!feof($furl)) {
        $content .= fgets($furl, 1024);
    }
}

$code = "/<h1><a href='(.*)' target='_blank'>(.*)<Va>/sU";
preg_match_all($code, $content, $urlrr);
foreach ($urlrr[1] as $k => $v) {
    echo "<a href=$v> " . $urlrr[2][$k] . "<br>" . "</a>";
    echo "&nbsp;&nbsp;" . $urlrr[1][$k] . "<br>";
    echo "<hr/>";
}

?>


<?php
//复制文件
$state = copy("D:\\test.txt", 'd:\\copy_test.txt');
if ($state) {
    echo "复制成功";
} else {
    echo "复制失败";
}

?>

<?php
//创建目录
$state = mkdir("D:\\test");
if ($state) {
    echo "创建目录成功";
} else {
    echo "创建目录失败";
}

//删除目录
$url = mkdir("D:\\test");
if (is_dir($url)) {
    echo "删除目录成功";
    rmdir($url);
} else {
    echo '目标非目录,类型为:' . filetype($url) . '删除成功';
    unlink($url);
}


?>

<p>递归删除目录</p>
<?php
function deleteDir($dir)
{
    $handle = @opendir($dir);
    readdir($handle);
    readdir($handle);
    while (false !== ($file = readdir($handle))) {
        $file = $dir . DIRECTORY_SEPARATOR . $file;
        if (is_dir($file)) {
            rmdir($file);
        } else {
            if (@unlink($file)) {
                echo " 文件<b>$file</b>删除成功<br/>\n\ ";
            } else {
                echo " 文件<b>$file</b>删除失败<br/>\n\ ";
            }
        }
    }
    if (rmdir($dir)) {
        echo " 目录<b>$dir</b>删除成功<br/>\n\ ";
    } else {
        echo " 目录<b>$dir</b>删除失败<br/>\n\ ";
    }
}

$dir = "d:\\test";
deleteDir($dir);
unlink($dir);

?>

<?php
$state=rename("d:\\test.txt","rename.txt");
if ($state){
    echo '重命名成功';
}

//移动文件
function move($source, $dest)
{
    $file = basename($source);
    $desct=$dest.DIRECTORY_SEPARATOR.$file;
    return rename($source, $desct);
}

move("test.txt","d:\\")


?>

<?php
function copyDir($dirFrom, $dirTo)
{
    if (is_file($dirTo)) {
        die("无法建立目录 $dirTo");
    }

    if (!file_exists($dirTo)) {
        mkdir($dirTo);
    }

    $handle = opendir($dirFrom);
    readdir($handle);
    readdir($handle);

    while (false !== ($file = readdir($handle))) {
        $fileFrom=$dirFrom.DIRECTORY_SEPARATOR.$file;
        $fileTo=$dirTo.DIRECTORY_SEPARATOR.$file;

        if (is_dir($fileFrom)){
            copyDir($fileFrom,$fileTo);
        }else{
            @copy($fileFrom, $fileTo);
        }
    }
}

 //使用
copyDir("test","copydir");
?>

<p>遍历目录</p>
<?php
$url="d:\\";
$handle = opendir($url);

echo '<table border="0" width="800" cellspacing="0" cellpadding="0"';
echo '<th align="left" bgcolor="#ffe4c4">';
echo '<th>文件名</th>
<th>文件大小</th>
<th>文件类型</th>
<th>创建时间</th>
<th>修改时间</th>
<th>是否可读</th>
<th>是否可写</th>
<th>可执行</th>
</tr>
';

while ($file = readdir($handle)) {
    $dirFile=$url."/".$file;
    echo "<tr>";
    echo '<td>'.$file.'</td>';
    echo '<td>'.filesize($dirFile).'</td>';
    echo '<td>'.filetype($dirFile).'</td>';
    echo '<td>'.date("Y/n/t",filectime($dirFile)).'</td>';
    echo '<td>'.date("Y/n/t",filemtime($dirFile)).'</td>';
    $read=is_readable($dirFile) ? "支持" : "不支持";
    $writ=is_writable($dirFile) ? "支持" : "不支持";
    $exe=is_executable($dirFile) ? "支持" : "不支持";

    echo "<td align='center'>".$read."</td>";
    echo "<td align='center'>".$writ."</td>";
    echo "<td align='center'>".$exe."</td>";

    echo "</tr>";
}
echo "</table>";

closedir($handle);

?>

 <p>PHP文件的上传</p>

<form action="index.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
    选择文件:<input type="file" name="files">
    <input type="submit" value="上传">
</form>

<?php

//单文件上传
//存储目录
$file = "D:\\test";
//文件上传后完整路径和名称
$url=$file.$_FILES["files"]["name"];
if (move_uploaded_file($_FILES["files"]["tmp_name"], $url)) {
    echo "文件上传成功!<br/>";
}else{
    echo "文件上传失败!<br/>";
    print_r($_FILES);
}

?>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人海中的海盗

你的打赏将是对我的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值