Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Echo last updated file

  1. #1
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Echo last updated file

    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

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    PHP Code:
    <?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.
    ?>
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    william james (12-28-2010)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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 Code:
    <?php
    echo phpinfo();
    ?>
    or

    PHP Code:
    <?php
    echo 'Current PHP version: ' phpversion();
    ?>
    Corrections to my coding/thoughts welcome.

  5. #4
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Might be the directory calling

    PHP Code:
    <?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];'
    ?>
    Corrections to my coding/thoughts welcome.

  7. #6
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  8. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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?
    Corrections to my coding/thoughts welcome.

  9. #8
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    My site is in a subdomain- xxx.mysite.com
    The txt files is located at public_html/xxx/video/archives/


  10. #9
    Join Date
    May 2010
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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?

  11. #10
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    PHP 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_NUMERICSORT_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";
      }
    ?>
    Jeremy | jfein.net

  12. The Following User Says Thank You to Nile For This Useful Post:

    william james (12-29-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •