php 读写文件文本

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 读写文件文本相关的知识,希望对你有一定的参考价值。

写入

/**
 * @param $login
 * @param $user
 * @param $search
 */
function recordSearchEmpty($login, $user, $search)

    $uploadFile = $_SERVER['DOCUMENT_ROOT'] . "/static/file/text_cache/search_txt.txt";
    $title = "------------------------\\n";
    $userInfo = "";
    if ($login) 
        $userInfo .= "用户#" . $user['username'] . "#_OVER\\n";
        $userInfo .= "邮箱#" . $user['email'] . "#_OVER\\n";
    
    $currentTime = "时间#" . date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']) . "#_OVER\\n";
    $content = $title . $userInfo . $currentTime . "搜索内容#" . $search . "#_OVER\\n";
    file_put_contents($uploadFile, urldecode($content), FILE_APPEND);

读取

<?php
/**
 * Created by PhpStorm.
 * User: liuan
 * Date: 2018/11/29
 * Time: 23:46
 */
header('content-type:text/html;charset=utf-8');
include_once 'lib/fun.php';
include_once 'la_config.php';
if ($login = checkLogin()) 
    $user = $_SESSION['user'];

$admin = $user['username'] == ADMIN_USER;

date_default_timezone_set('PRC');
$filename = $_SERVER['DOCUMENT_ROOT'] ."/static/file/text_cache/search_txt.txt";
$content = file_get_contents($filename, FILE_USE_INCLUDE_PATH);
//$content = str_replace("\\n", "<br/>", $content);
$time = isset($_POST['time']) ? $_POST['time'] : "";
$search = isset($_POST['search']) ? $_POST['search'] : "";
//删除包含$time and $search的内容
//显示txt所有数据
$data = initData($content);


if (!empty($time) && !empty($search)) 
//    从0开始 所以此处写-1
    $currentIndex = -1;
    $replaceStr = '';
    foreach ($data as $v) 
        $currentIndex++;
        if ($v['time'] == $time && $v['search'] == $search) 
            //删除这个索引的内容 我们使用replcae 删除
            $title = "------------------------\\n";
            $userInfo = "";
            if ($v['username']) 
                $userInfo .= "用户#" . $v['username'] . "#_OVER\\n";
            
            if ($v['email']) 
                $userInfo .= "邮箱#" . $v['email'] . "#_OVER\\n";
            
            $currentTime = "时间#" . $v['time'] . "#_OVER\\n";
            $replaceStr = $title . $userInfo . $currentTime . "搜索内容#" . $v['search'] . "#_OVER\\n";
            break;
        
    

    $fp = fopen($filename, "w");
    $finalData = str_replace($replaceStr, '', $content);

    $len = fwrite($fp, $finalData);
    $fp . flush();
//    删除了数据 再次刷新一次
    $data = initData($finalData);



/**
 * @param $content
 * @param $data2
 * @return array
 */
function initData($content)

    $splite = '------------------------';
    $data1 = array_filter(explode($splite, $content));
    $data = array();
    foreach ($data1 as $value) 
        $data2 = array();
        $data2['username'] = getNeedText("用户", $value);
        $data2['email'] = getNeedText("邮箱", $value);
        $data2['time'] = getNeedText("时间", $value);
        $data2['search'] = getNeedText("搜索内容", $value);
        $data[] = $data2;
//    $data['']=
    
    return array_reverse($data);



function getNeedText($str, $allText)

    $res = '';
    if (strstr($allText, $str)) 
        $date = explode($str . '#', $allText);
        $date = explode('#_OVER', $date[1]);
        $res = $date[0];
    
    return $res;



?>
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <title>搜索历史</title>
    <link rel="stylesheet" type="text/css" href="static/css/common.css"/>

    <link rel="stylesheet" type="text/css" href="static/css/detail.css"/>
    <link rel="stylesheet" type="text/css" href="static/lib/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="static/lib/jquery/2.1.1/js/jquery.min.js"></script>
    <script src="static/lib/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<?php getHeader();?>
</head>
<body>


<!--导航栏-->
<?php nav($login,$user,$admin)?>

<div class="so">
    <!-- <p class="mg-b20 text-center font16"><a href="register.php">注册</a></p>-->


    <h2 class="text-center font32"><a href="commit.php">课程需求登记</a></h2>
    <?php if (count($data) <= 0): ?>
        <p style="font-size: 16px" class="text-center">暂时还没有搜索历史 <a
                    href="index.php">去搜索</a></p>
    <?php else: ?>
        <div class="mg-t20">
            <h3>用户搜索历史</h3>
            <table border="1">
                <tr class="row">


                    <?php if ($admin): ?>
                        <th class="col-1 col-lg-1 col-md-1 col-xs-1">id</th>
                        <th class="col-2 col-lg-2 col-md-2 col-xs-2">用户</th>
                        <th class="col-4 col-lg-4 col-md-4 col-xs-4">搜索内容</th>
                        <th class="col-2 col-lg-2 col-md-2 col-xs-2">时间</th>
                        <th class="col-2 col-lg-2 col-md-2 col-xs-2">联系方式</th>
                        <th class="col-1 col-lg-1 col-md-1 col-xs-1"></th>
                    <?php else: ?>
                        <th class="col-3 col-lg-3 col-md-3 col-xs-3">id</th>
                        <th class="col-6 col-lg-6 col-md-6 col-xs-6">搜索内容</th>
                        <th class="col-3 col-lg-3 col-md-3 col-xs-3">时间</th>
                    <?php endif; ?>
                </tr>
                <?php foreach ($data as $v):
                    $i++;
                    ?>

                    <tr class="row">

                        <?php if ($admin): ?>
                            <td class="col-1 col-lg-1 col-md-1 col-xs-1"><?php echo $i ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['username'] ?></td>
                            <td class="col-4 col-lg-4 col-md-4 col-xs-4"><?php echo $v['search'] ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['time'] ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['email'] ?></td>
                            <td class="col-1 col-lg-1 col-md-1 col-xs-1">
                                <form action="search.php" name="form_delete" id="form_delete" method="post">
                                    <input type="hidden" name="time" value="<?php echo $v['time'] ?>">
                                    <input type="hidden" name="search" value="<?php echo $v['search'] ?>">
                                    <button type="submit" class="btn btn-danger delete">删除</button>
                                </form>
                            </td>
                        <?php else: ?>
                            <td class="col-3 col-lg-3 col-md-3 col-xs-3"><?php echo $i ?></td>
                            <td class="col-6 col-lg-6 col-md-6 col-xs-6"><?php echo $v['search'] ?></td>
                            <td class="col-3 col-lg-3 col-md-3 col-xs-3"><?php echo $v['time'] ?></td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div> 
    <?php endif; ?>

</div><!--row end-->
<script>
    // $(function () 
    //     $('#form_delete').each(function (i) 
    //         console.log('i' + i)
    //
    //     )
    //
    // );
</script>

</body>


</html>

以上是关于php 读写文件文本的主要内容,如果未能解决你的问题,请参考以下文章

php 怎么修改txt文本

php中并发读写文件冲突的解决方案(文件锁应用示例)

关于PHP文件操作: php保存数据到文本文件,怎么弄?

在LINUX 下 使用PHP 修改文本文件

c#中文件读写StreamWriter问题

php 导入/导出 csv 文件