How to acces a specified line of a textfile?

Discussion in 'PHP' started by tilto, Mar 29, 2008.

  1. #1
    Hi,
    i need some help doing the following:
    I got a picturegallery, and those pictures are displayed by a script in thumbnail/fullsize. The images are stored in different folders. Now i want to add a description for some of theese pictures, which should be stored in a .txt (no database). The textfile is in the appropriate folder and looks like this:
    file011.jpg
    Description to file1
    file02.jpg
    description file2
    ....
    now i want to print the description linked to the picture by using $filename. could someone tell me how to do that please?
    thanks in advance
     
    tilto, Mar 29, 2008 IP
  2. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Out of interest, is there any reason you're against using a database for this? It'll be a lot more efficient (and also easier).
     
    Xtrm2Matt, Mar 29, 2008 IP
  3. tilto

    tilto Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes, the main reason is that i am new to this and have no time to setup a database right now / learning how to do it.
    i thought it could be just some lines of code for me to do the job since those textfiles dont have more than 30 lines right now.
     
    tilto, Mar 29, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    By the time you work out bugs with duplicate filenames & whatnot, you'ld have used less time learning to do it with a database.
     
    joebert, Mar 29, 2008 IP
  5. owenj

    owenj Guest

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You will probably want a text file for each folder. You can use
    
       $var = file_get_contents('filename'); 
    
    Code (markup):
    to put the contents of the file into the $var variable. You may have to explode the variable to create an array and then search for the file name in that array. It will be quite messy and will probably be easier to use a database.
     
    owenj, Mar 30, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    The file() command loads a file into an array automatically (line by line). You can then while/for loop it, and print each file & description,.

    Jay
     
    jayshah, Mar 30, 2008 IP