Log in

View Full Version : Echo last updated file



william james
12-27-2010, 01:33 PM
Another php question :)

I have 3 .txt files in a folder with div contents.
I want to show the latest updated .txt file into my homepage.
I found a code which is very close to the outcome I wanted....

I have this code:


<?php
$files = glob( 'video/archives/*.*' );

// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);

// do whatever you want with $files

print_r( $files );
?>


the outcome is:


Array ( [0] => video/archives/1.txt [1] => video/archives/2.txt [2] => video/archives/3.txt )


-------------------

the code prints the last updated .txt files.
But I want to show the content of the .txt files instead of just printing it...

maybe I need to replace this code:


print_r( $files );

to something...
but don`t know how...

Thankyou in advance :)

bluewalrus
12-27-2010, 04:24 PM
<?php
$dir = 'video/archives';
$files = glob( $dir . '/*.*' );
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);
for ($count_files = 0; $count_files < count($files); $count_files++) {
$file_contents[] = file_get_contents($dir . '/' . $files[$count_files]);
}
//then you should have contents of each file in the $file_contents array. If you want the most recent 'echo $file_contents[0];'
//if you want all 3 files you can use a for loop like I did or a foreach loop.
?>

bluewalrus
12-28-2010, 03:33 AM
What version of php are you running, did you get any error messages when you tried the code I entered, and did you add in the echo in the comment (denoted by the // at the start of the line)? To be exact your goal is to have the contents of 'text2.txt' displayed on the page assuming it is the most recently updated file and if 'text1.txt' is updated later that would be displayed automatically?

To get the version of php you are running you can make a blank php file and put in


<?php
echo phpinfo();
?>


or


<?php
echo 'Current PHP version: ' . phpversion();
?>

william james
12-28-2010, 05:10 AM
thanks sir!

yes, I want the last updated file to show :)

Current PHP version: 5.2.14

I pasted exactly the code you given me and the error is this:



Warning: file_get_contents(video/archives/video/archives/1.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/teentvin/public_html/ddd/index.php on line 31

bluewalrus
12-28-2010, 05:46 AM
Might be the directory calling


<?php
$dir = 'video/archives';
$files = glob( $dir . '/*.*' );
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);
for ($count_files = 0; $count_files < count($files); $count_files++) {
$file_contents[] = file_get_contents('/' . $dir . '/' . $files[$count_files]);
}
echo $file_contents[0];'
?>

william james
12-28-2010, 07:32 AM
Hi sir,
After inserting the last code you provide...


Warning: file_get_contents(/video/archives/video/archives/teenburg.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/teentvin/public_html/xxx/index.php on line 31

Warning: file_get_contents(/video/archives/video/archives/tinseks.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/teentvin/public_html/xxx/index.php on line 31

Warning: file_get_contents(/video/archives/video/archives/teenamite.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/teentvin/public_html/xxx/index.php on line 31

:)

bluewalrus
12-29-2010, 06:23 AM
It looks like the directory is not being called correctly. Where is videos located in your directory set up is it at the root i.e. public_html?

william james
12-29-2010, 11:16 AM
My site is in a subdomain- xxx.mysite.com
The txt files is located at public_html/xxx/video/archives/

:)

william james
12-29-2010, 11:16 PM
After a series of trial and error, the code finally works!


<?php
$files = glob( 'video/archives/*.*' );

// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);
for ($count_files = 0; $count_files < count($files); $count_files++) {
$file_contents[] = file_get_contents('' . $dir . '' . $files[$count_files]);
}
echo $file_contents[0];
?>

However I need more features--limit the number of lines to show on that code :)

I have a code which shows 10 number of lines from a txt file.

<?php

$file = file( 'archives/teenamite.txt' );
$limit = 9;

for ($i = 0; $i <= $limit; $i++ ){
echo $file[$i] . "\n";
}

?>

How to combine the two codes? :)

Nile
12-29-2010, 11:26 PM
<?php
$files = glob('video/archives/*.*');
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(array_map('filemtime', $files), SORT_NUMERIC, SORT_DESC, $files);
for ($count_files = 0; $count_files < count($files); $count_files++) {
$file_contents[] = $dir . '' . $files[$count_files];
}
$file = file($file_contents[0]);
$limit = 9;

for ($i = 0; $i <= $limit; $i++) {
echo $file[$i] . "\n";
}
?>

william james
12-29-2010, 11:46 PM
@NEIL, Million thanks to you!:)

Nile
12-29-2010, 11:47 PM
No problem, I'm glad to help :D

Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
1. Go to your first post
2. Edit your first post
3. Click "Go Advanced"
4. In the dropdown next to the title, select "RESOLVED"

william james
12-30-2010, 12:28 AM
wew I think this is my ultimate goal, to show first 10 lines for each txt files from a folder into my homepage!

Please help me again :)

Nile
12-30-2010, 12:32 AM
So, type in a directory and get all the files in that folder and then display the first 10 lines of each file on a page?

<?php
$dir = "texts"; // the directory name

$glob = glob($dir."/*.*"); // get all the files with a 'name.extension' name
$files = array_combine($glob, array_map("file", $glob));

foreach($files as $key=>$value){
echo "<fieldset><legend>$key</legend>".nl2br(implode(array_slice($value, 0, 10)))."</fieldset>";
}
?>