How can I get the contents of a file one line at a time?

KingDave212

I have a project where a user uploads a photo with a name and caption, and I am having trouble with displaying the name and caption.

Right now, it's displaying the entire text file with each image, and I am having trouble fixing this.

My code so far:

    <?php
        $Dir = "files";
        $DirEntries = scandir($Dir);
        foreach ($DirEntries as $Entry)
        {
            if((strcmp($Entry, '.') != 0) && (strcmp($Entry, '..') != 0))
            {
                echo "<p>Name: " . file_get_contents("imagelist.txt") . "</p>";
                echo "<a href=\"files/" . $Entry . "\" target=\"_blank\" >" . $Entry . "</a><br />\n";  
            }
        }
        closedir($DirOpen);         
    ?>

Any help would be greatly appreciated.

JuanSedano

You can use fgets():

$inputFile = fopen("file.txt", "r");
if ($inputFile) {
    while (($line = fgets($inputFile)) !== false) {
        echo $line."<br>";
        // The process read the file line by line        
    }
} else {
    echo "There was an error in the opening file";
} 
fclose($inputFile);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I read one line at a time from a trio ReceiveStream?

How can I get the count of line in a file in an efficient way?

ls command: how can I get a recursive full-path listing, one line per file?

How can I handle the warning of file_get_contents() function in PHP?

How can i mock the contents of a .cs file with Pester allowing me to iterate through each line and each character?

How can I get the value of a group and define it in one line in JavaScript?

How can i get the sum of a line from a .txt file in powershell?

How can I get 3 divs in one line? Flex not working

how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?

How can I display the contents of a text file on the command line?

How can I view the contents of tar.gz file without extracting from the command-line?

How can I get a specific line from a file?

How can I get octal file permissions from command line?

How can I get this line from a big file in shell

How can I evaluate a math equation, one per line in a file?

How can I pass variables to a script one line at a time in bash?

How do I find a time stamp on a file and then echo it into the contents of the file as the first line?

How can I call only one line of an html file?

How can I get binary file permissions from command line?

I have a list of files, how can I open and copy the contents of each file to a new one using bash?

How can I get my C program to read more than one line of text from a file?

How can I get an XML attribute as below using file_get_contents or simplexml_load_file?

How to work on one line of a file at a time in python

How can I get the contents of a specific line printed out to console?

How can I pass one process to another using pipe (read file contents-get contents-create new file)?

How can I get the time lenght of a sound file (.wav) in python?

How can I limit the output of Get-ChildItem to one line

Using node/discord.js, how can I get my discord bot to parse an XML file and produce contents from within one of its tags as a response?

How can I get all text box entries into one file?

TOP Ranking

HotTag

Archive