php开发之文件读取、写入

发布时间 2023-11-11 14:23:04作者: 夜未至

前言

续之前的系列,这里php开发的文件操作的内容读取以及文本写入的部分

文件读取代码的实现

css代码

本系列的php博客都是这个css,名字都是index.css

/* css样式初始化 */
* {
    font-family: 'Poppins', sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    outline: none;
    border: none;
    text-decoration: none;
    text-transform: capitalize;
    transition: .2s linear;
}

html {
    font-size: 62.5%;
}
/* header样式初始化*/
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: #3F3D56;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header .logo {
    color: white;
    padding: 0 1rem;
}
/* 导航样式 */
.navbar ul{
    display: flex;
}

.navbar ul li{
    font-weight: bold;
    width: 128px;
    list-style: none;
    text-align:center;
}
.navbar ul li a {
    width: 128px;
    line-height: 45px;
    font-size: 1.7rem;
    color: white;
}
.navbar ul li a:hover{
    color: #009933;
}
/* 二级菜单样式 */
.navbar ul li ul{
    position: absolute;
    display: none;
    width: 128px;
    line-height: 45px;
}

.navbar ul li ul li{
    background-color: #3F3D56;
}
/* 悬浮展开二级菜单 */
.navbar ul li ul li a:hover{
    color: white;
}
.navbar ul li ul li:hover{
    background-color: #009933;
}

.navbar ul li:hover ul{
    display: block;
}
/* 网站内容*/
.content{
    margin-top:50px;
}

/*让文件下载的文件列表居中显示*/
.download-links {
    text-align: center; /* 使链接居中 */
    font-family: 'LiSu', 'STLiti', cursive; /* 尝试使用隶书字体,如果不可用则退回到通用草书字体 */
    font-style: italic; /* 设置字体为斜体 */
}

.download-links ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: inline-block; /* 使 <ul> 内容居中对齐 */
}

.download-links li {
    margin: 10px; /* 添加一些间距 */
}

.download-links a {
    font-size: 18px; /* 可以根据需要调整大小 */
    text-decoration: none;
    color: #000;
}

.download-links a:hover {
    text-decoration: underline;
}

文件内容的读取

新建一个文件file_read.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Head</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
<div class="header">
    <a href="index.html" class="logo">
        <h1>wushiyiwuzhong</h1>
    </a>
    <nav class="navbar">
        <ul>
            <li><a href="file_index.php">文件功能导航</a></li>
            <li><a href="file_upload.php">文件上传</a></li>
            <li><a href="file_downlaod.php">文件下载</a></li>
            <li><a href="#">文件删除</a></li>
            <li><a href="#">文件读取</a></li>
            <li><a href="#">文件写入</a></li>
        </ul>
    </nav>
</div>

<div class="download-links">
    <ul>
        <?php
        $files = scandir('.');//扫描当前目录
        $files = array_filter($files, function($file) {
            return is_file($file);
        });

        foreach ($files as $file) {
            // 创建下载链接,指向 download.php,并传递 filename 参数
            echo '<li><a href="file_read.php?filename=' . urlencode($file) . '">' . htmlspecialchars($file) . '</a></li>';
        }
        ?>
    </ul>
</div>
</body>
</html>

<?php
// 读取 filename 参数
$filename = isset($_GET['filename']) ? $_GET['filename'] : '';

$filePath = './' . $filename;

// 检查文件是否存在且可读
if (file_exists($filePath) && is_readable($filePath)) {
    $content = file_get_contents($filePath); // 读取文件内容
    echo nl2br(htmlspecialchars($content)); // 将内容转换为适合在 HTML 中显示的格式
} else {
    echo "无法读取文件";
}

下面是运行效果

随意读取一个文件进行测试

成功读取当前文件的文件内容,尝试读取其他文件

http://localhost:63342/wushiyiwuzhong.com/file_read.php?filename=../test.txt


成功读取