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.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名