Results 1 to 7 of 7

Thread: PHP Help

  1. #1
    Join Date
    May 2011
    Location
    Manchester, UK
    Posts
    12

    Post PHP Help

    Hey,

    I'm not sure what section this should go in but here it goes anyway.

    I need to basically grab some data from an external .txt file. I only need the bottom line of the file. I know a code which will echo the top value of the file, anyone know how to change this to the bottom value?

    Here's the code:

    Code:
    $myFile = "testFile.txt";
    $fh = fopen($myFile, 'r');
    $theData = fgets($fh);
    fclose($fh);
    echo $theData;
    Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    334
    Hi there,

    Here's one (efficient) way to achieve what you want :

    PHP Code:
    $handle fopen('file.txt''r');

    $pos = -1;

    // Seeks file's end.
    fseek($handle$posSEEK_END);

    $string '';
    $char fgetc($handle);

    // Read every characters of the file, one by one, from the end,
    // until an end of line delimiter is found or until we've reached
    // begining of the file.
    while (($char !== false) && ($char != "\r") && ($char != "\n"))
    {
        
    fseek($handle$pos--, SEEK_END);
        
    $char fgetc($handle);
        
    $string $char $string;
    }

    fclose($handle);

    echo 
    $string

  3. #3
    Join Date
    May 2011
    Location
    Manchester, UK
    Posts
    12
    Hey WootWoot,

    I really appreciate your reply. I have tried out this code, looks like it should work and I got some kind of PHP error. It doesn't make much sense to me can you make anything out of it?

    Warning: fseek() [function.fseek]: stream does not support seeking in /home/directory/index.php on line 7

    Warning: fseek() [function.fseek]: stream does not support seeking in /home/directory/index.php on line 17

  4. #4
    Join Date
    Apr 2008
    Posts
    334
    Hum... This piece of code shall work fine.

    Have you replaced "file.txt" with your own file name? Is it a .txt file? Are you sure this file exists and is properly CHMODed?

    If the file isn't that big, you may simply use :

    PHP Code:
    $lines file('file.txt');
    echo 
    $lines[count($lines) - 1]; 

  5. #5
    Join Date
    Apr 2011
    Location
    Charlotte, NC
    Posts
    104
    In the case of large files, I typically use this method:

    PHP Code:
    $file  'myfile.txt';
    $lines 1;

    $file  escapeshellarg($file);
    $lines escapeshellarg($lines);

    exec("tail -n $lines $file\n",$output); 
    The $output array, in this case, would contain the last line of myfile.txt. I find this method convenient for looking at apache log files, or any files that have lots of line breaks.

  6. #6
    Join Date
    May 2011
    Location
    Manchester, UK
    Posts
    12
    Hello,

    Thanks both of those worked. I appreciate your time in helping me out with this.

    CowHosts

  7. #7
    Thanks for the reply good solution
    Dubai Movers Packers and Movers Services

    uae hosting uae hosting

Similar Threads

  1. Replies: 33
    Last Post: 07-25-2011, 01:01 PM
  2. Windows- PHP exploit being used to render all PHP sites useless, need urgent help,!
    By webuser99 in forum Hosting Security and Technology
    Replies: 11
    Last Post: 03-24-2008, 05:44 AM
  3. PHP: Custom WHMCS billing gateway or PHP script - API included
    By omega36 in forum Employment / Job Offers
    Replies: 1
    Last Post: 11-29-2007, 03:01 PM
  4. php safe_mode on and /usr/lib/php/DB.php error - pear
    By nand in forum Hosting Security and Technology
    Replies: 1
    Last Post: 05-03-2005, 02:44 AM
  5. Replies: 1
    Last Post: 02-15-2005, 07:44 PM

Tags for this Thread

Posting Permissions

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