dtp87205 2012-07-03 14:59
浏览 75
已采纳

php从文件夹中获取文本文件内容

On my website, I have an upload story feature with a user written title and story. It uploads it as a text file in a directory. Is there a way to list the content of these files using php or anything? Also, I'd like to only show like 200 chars of the story, and have a 'show full story' button, that would show the full story (I'll use jQuery for this).

Here is my current code for this, though it doesn't work and i can't figure out the problem :(

<head>
    <style type="text/css">header {font-size:20pt; color:#ff00e8;} footer {text-align: left; font-weight:bold; padding-left: 5px;} article {padding:20px;}</style>    
</head>
<body>
<h2><a href="../php/upload_story.php" style="font-size:15pt; color:#ff00e8; text-decoration: none;" id="tortenetfeltolt">Van  egy  jó  történeted? Írd meg és kikerülhet az oldalra!</a></h2>

<?php
$dataArray = array();
//Number of chars for the string
$num = 200;

//Check if DIR exists
if ($handle = opendir('../php/biralas_tortenetek/')) {
    //Loop over the directory
    while (false !== ($file = readdir($handle))) {
        //Strip out the . and .. files
        if ($file != "." && $entry != "..") {
           // $dataArray[] = array();
            //Store file contents
            $filecontent = file_get_contents($file);
            //Split the content and store in array
            $length = strlen($filecontent);
            $dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length )); 

        }
    }
    //close the dir
    closedir($handle);
}


?>

<?php foreach($dataArray as $data) { ?>
    <div class="visible">
        <?php echo $data[0]; ?>
    </div> 
    <div class="hidden">
        <?php echo $data[1]; ?>
    </div>
<?php } ?>
</body>
  • 写回答

1条回答 默认 最新

  • duanliujie8639 2012-07-03 15:05
    关注

    I see you used: substr($filecontent, $num, $length );

    In the substr function, the second argument is the position you would like to start at, the third is the length. You specfied $num = 200, therefore substr will start at position 200.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备