Jump to content

View files on mainpage from a folder starting with the newest text file edited!


ztealmax

Recommended Posts

Hi i small question or big ;)

I wish to scan a folder for text files, and show textfiles on mainpage one at the time, begining with the newest file
edited, is this possible? (Maybe get files with edit dates and time? and there after display latest file)


i use this script for showing all files in a folder
[sup]<?
           if ($dir = @opendir("news/"))
           {
               while (($file = readdir($dir)) !== false)
               {
                   if($file != ".." && $file != ".")
                   {
                       $filelist[] = $file;
                   }
               }
               closedir($dir);
           }
           ?>
<form>
           <select name="selected_dir" >
           <?php
           asort($filelist);
           while (list ($key, $val) = each ($filelist))
           {
               echo "<option>$val</option>";
           }
           ?>
            </select>
</form> [/sup]
Link to comment
Share on other sites

[quote author=taith link=topic=116995.msg477020#msg477020 date=1164987641]
just as a suggestion... its ALOT faster to use glob() to get files :-)
[code]
<?
$filelist=glob("news/*.txt");
?>
[/code]
[/quote]
Thanx , yeaa just tried it mush better :P im a bit of a noob ;) anyway to open files to read files starting with the newest first, and set others as number links?

BTW im not using sql ;) so you all know :p


:D
Link to comment
Share on other sites

how do i make it so it reads the latest edited file in that folder?
i meen like it scans all files in the folder then opens the latest file for viewing, and after that it sort the rest of the files in date order as number links for ex:

[img]http://cms.indivi.se/filesread.png[/img]
Yea iknow im a noob ;)

thinking of trying to smash codes togheter got these right now:

scanfolder.php
[code]<?
$filelist=glob("news/*.txt");
?>
           <form>
           <select name="selected_dir" >
           <?php
           asort($filelist);
           while (list ($key, $val) = each ($filelist))
           {
               echo "<option>$val</option>";
           }
           ?>
            </select>
</form>
[/code]

read.php
[code]<?php
$filename = 'news/news.txt';
if (file_exists($filename)) {
   echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
// set file to read
$file = 'news/news.txt';
// open file
$fh = fopen($file, 'r') /*or die('Could not open file!')*/;
// read file contents
$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fh);
// print file contents
echo $data;
?>[/code]
As you can see i can only open one file in this read script, and that is what i tell it to open
in this example i use [i]news.txt[/i]

Just not sure how im suppose to do it ;)
Link to comment
Share on other sites

[code=php:0]
<?php
// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
  echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?> [/code]
Link to comment
Share on other sites

yea i got it to tell me what date is was latest modified, what im after is that it opens the file
for view/read from the folder news/ and only the file that was last modified / edited
and places all other content as links as i demostrated on pic above :)

Im sorry if im not clear to what i meen , my english isnt that good :)
Link to comment
Share on other sites

oy... enough of this... improvising time!

[code]
<?
if($dir = @opendir("news/")){
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
  $date=date ("YmHis.", filemtime($filename));
  $filelist['$date'] = $file;
  }
}
closedir($dir);
}
?>
<form>
<select name="selected_dir" >
<?php
ksort($filelist);
while(list ($key, $val) = each ($filelist)){
echo "<option>$val</option>";
}
?>
</select>
</form>
[/code]
hows that work? you might need a different sort...
Link to comment
Share on other sites

[quote author=taith link=topic=116995.msg477108#msg477108 date=1164994911]
oy... enough of this... improvising time!

[code]
<?
if($dir = @opendir("news/")){
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
   $date=date ("YmHis.", filemtime($filename));
   $filelist['$date'] = $file;
  }
}
closedir($dir);
}
?>
<form>
<select name="selected_dir" >
<?php
ksort($filelist);
while(list ($key, $val) = each ($filelist)){
echo "<option>$val</option>";
}
?>
</select>
</form>
[/code]
hows that work? you might need a different sort...
[/quote]

it work as such as it only showed file ending with 4 / news4.txt
http://cms.indivi.se/test.php as you can see here :)

[img]http://cms.indivi.se/filesread2.png[/img]

Im sorry if im such a no0b :D
Link to comment
Share on other sites

i did say there may need a different sort(asort,ksort...)

to further it along...
[code]<?
if($dir = @opendir("news/")){
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
  $i++;
  $k=str_pad($i,7, "0",STR_PAD_LEFT);
  $date=date ("YmHis.", filemtime($filename));
  $filelist['$date'] = $file;
  }
}
closedir($dir);
}
?>
<form>
<select name="selected_dir" >
<?php
ksort($filelist);
while(list ($key, $val) = each ($filelist)){
echo "<option>$val</option>";
}
?>
</select>
</form>[/code]
Link to comment
Share on other sites

Thanx sorry my english isnt that good ;)

im testing now:

http://cms.indivi.se/test.php

ok tested it now, it worked :) but i had to rename files so they dont contain any numbers in the names, when i did that
the date thingy worked great :D

thanx m8

Now how do i sort the remaining files as a text link [1][2].....
You got any idea? :D

Link to comment
Share on other sites

[code]<?
if($dir = @opendir("news/")){
$i=0;
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
  $i++;
  $k=str_pad($i,7, "0",STR_PAD_LEFT);
  $date=date("YmHis", filemtime($file)).$k;
  $filelist[$date] = $file;
  }
}
closedir($dir);
}
?>
<form>
<select name="selected_dir" >
<?
krsort($filelist);
while(list($key, $val) = each($filelist)){
echo "<option>$val</option>";
}
?>
</select>
</form>[/code]
Link to comment
Share on other sites

Im sorry im just not getting it to work :(

it stills seems to sort by filename

btw can i somehow change:

[code]<form>
<select name="selected_dir" >
<?
krsort($filelist);
while(list($key, $val) = each($filelist)){
echo "<option>$val</option>";
}
?>
</select>
</form>[/code]

to have this instead:
[code]<?php
$filename = 'news/news.txt';
if (file_exists($filename)) {
   echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
// set file to read
$file = 'news/news.txt';
// open file
$fh = fopen($file, 'r') /*or die('Could not open file!')*/;
// read file contents
$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fh);
// print file contents
echo $data;
?>[/code]

ofcource filename equal what the date tells it should be $val i guess?
Link to comment
Share on other sites

[quote author=taith link=topic=116995.msg477142#msg477142 date=1164998183]
no... on that webpage, its sorting by reverse key... xtx-->ztx-->xt that should be right... no? if its backwords... change krsort to ksort...
[/quote]

ohh ok, i thought we where trying to only show the file last modified? ;) "check it by date then show that file"
Link to comment
Share on other sites

with that code... you cant resort it down again... just put a limiter on how many it shows...
[code]<?
if($dir = @opendir("news/")){
$i=0;
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
  $i++;
  $k=str_pad($i,7, "0",STR_PAD_LEFT);
  $date=date("YmHis", filemtime($file)).$k;
  $filelist[$date] = $file;
  }
}
closedir($dir);
}
krsort($filelist);
while(list($key, $val) = each($filelist)){
if(!isset($p)){
  $p=x;
  echo $val;
}
}
?>
[/code]
Link to comment
Share on other sites

[quote author=taith link=topic=116995.msg477150#msg477150 date=1164999046]
with that code... you cant resort it down again... just put a limiter on how many it shows...
[code]<?
if($dir = @opendir("news/")){
$i=0;
while(($file = readdir($dir)) !== false){
  if($file != ".." && $file != "."){
   $i++;
   $k=str_pad($i,7, "0",STR_PAD_LEFT);
   $date=date("YmHis", filemtime($file)).$k;
   $filelist[$date] = $file;
  }
}
closedir($dir);
}
krsort($filelist);
while(list($key, $val) = each($filelist)){
if(!isset($p)){
  $p=x;
  echo $val;
}
}
?>
[/code]
[/quote]

ahh okej...
Link to comment
Share on other sites

hmm ok, but what i really are after is for it to open and read the file that was last modified in the folder!
for example are there 3 files:
file.txt      modified 2006-12-01 22:00
test.txt    modified 2006-12-01 22:05
gurka.txt  modified 2006-12-01 22:10

So what i need the script to do is open file names gurka.txt as that is the one last modified
Link to comment
Share on other sites

Now i have the date thingy working
This gives me a list of all files in my folder news/
and sets the latest file edited on the first row:
[code]<?php
$this = $PHP_SELF;
$dir  = $DOCUMENT_ROOT."news/";

$files = opendir($dir);
$file_list = array();
$file_sort = array();

if(empty($sort))$sort="name";
if(empty($r)) $r=0;

$cnt = 1;

    while ($file = readdir($files))
{
$full_path = $dir."/".$file;
if(!is_dir($full_path))
{
$ext = explode(".",$file);
$i=count($ext);
if($i>1)$file_details["ext"] = strtolower($ext[$i-1]);
$file_details["name"] = $ext[0];
$file_details["date"] = filectime($full_path);
$file_list[$cnt] = $file_details;

$key = strtolower($file_details[$sort]);
$file_sort[$cnt] = $key;

$cnt++;
}
}

if($r)arsort($file_sort);
else asort($file_sort);


?>
<link href="../webmax.css" rel="stylesheet" type="text/css">

<table width="465" border="0" cellpadding="3" cellspacing="0" class="bodytext">
  <tr>
    <td width="174"><a href="<?php print($this);?>?sort=name&r=<?php print(!$r);?>">Name</a></td>
    <td width="141"><a href="<?php print($this);?>?sort=date&r=<?php print(!$r);?>">Date</a></td>
  </tr>
  <tr bgcolor="#0033CC">
    <td height="3" colspan="5"> </td>
  </tr>
<?php
while(list($key, $value)=each($file_sort))
{
$value = $file_list[$key];
?>
  <tr>
    <td width="174"><?php print($value["name"]);?></td>

    <td width="141"><?php print(date("Y/m/d H:i",$value["date"]));?></td>
  </tr>
<?php
}
?>
</table>[/code]

Now for a follow up question
How can i now read only that file (latest modified) with this code (needs modification):
[code]<?php
$filename = 'news/*.*';

// set file to read
$file = 'news/info.txt';
// open file
$fh = fopen($file, 'r') /*or die('Could not open file!')*/;
// read file contents
$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fh);
// print file contents
echo $data;
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.