Reading only x file line

Discussion in 'PHP' started by ignas2526, Dec 6, 2008.

  1. #1
    Hello,
    I tried to search but found nothing.
    I need to read only x file line whiteout reading all other lines, is there any such functions?
    Thanks.
     
    ignas2526, Dec 6, 2008 IP
  2. Ueland

    Ueland Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if it is a large file it`s simplest to do a shell_exec and get the first bit of the file.

    If its a small file you want the first line of you can do:

    $foo = file("filename");
    $firstLine = $foo[0];

    voila.
     
    Ueland, Dec 6, 2008 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well that's not what i was looking for. It reads whole first.
    I have php file witch content like this:
    There will be like 500-600lines... so each time then i need to read only one line i will need to read all lines first.
    I already have function like that. I need only to read x line in whatever place.
     
    ignas2526, Dec 6, 2008 IP
  4. raimis100

    raimis100 Banned

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $foo = file("filename");
    $firstLine = trim ($foo[$X]); < -- line number in X
     
    raimis100, Dec 7, 2008 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    I think what you need is eval(); expressing a string as code.
    
    $foo = file("filename", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $firstLine = $foo[0];
    eval($firstLine);
    
    echo $l; // user1|pass|84.32.1.2|0|3|100
    
    PHP:
    note: you may need to remove the <? and ?>
     
    Kaizoku, Dec 7, 2008 IP
  6. lordofthelake

    lordofthelake Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I don't think there is a solution to read your file in random access (without loading it as a whole), unless you have lines of a fixed length (you may use fseek() in that case).
    If your goal is to speed up your script, why don't you use a SQLite database (support is built-in for PHP5)?
    You'll have both the advantages of a structured database and a plain file.

    Just my 2 cents.
     
    lordofthelake, Dec 7, 2008 IP