Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read a .TXT file with PHP?

Tags:

file

php

As I start the process of writing my site in PHP and MySQL, one of the first PHP scripts I've written is a script to initialize my database. Drop/create the database. Drop/create each of the tables. Then load the tables from literals in the script.

That's all working fine! Whoohoo :-)

But I would prefer to read the data from files rather than hard-code them in the PHP script.

I have a couple of books on PHP, but they're all oriented toward web development using MySQL. I can't find anything about reading and writing to ordinary files.

Yes, I know there's a gazillion questions here on stackoverflow about reading TXT files, but when I look at each one, they're for C or C# or VB or Perl. I'm beginning to think that PHP just can't read files :-(

All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:

program readfile;
handle = open('myfile.txt');
data = read (handle);
while (not eof (handle)) begin
    display data;
    data = read (handle);
    end;
close (handle);
end;     

I will also need to write files on the server when I get to the part of my site where people upload avatars, and save them as JPG or GIF files. But that's for later.

Thanks!

like image 437
Fredashay Avatar asked Mar 25 '12 16:03

Fredashay


People also ask

How we can read a text file in PHP?

PHP Read File - fread() The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

How do I open a read file in PHP?

PHP fopen() function is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.

Can PHP read HTML file?

Make a PHP file to read HTML content from a text filetxt' file in read mode and then use fread() function to display file content. You may also like read and delete file from folder using PHP. That's all, this is how to read HTML content from text file using PHP.

Which code reads the entire content of a file in PHP?

Read File using readfile() The readfile() function reads the entire content of a file and writes it to the output buffer.


2 Answers

From the PHP manual for fread():

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>

EDIT per the comment, you can read a file line by line with fgets()

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>
like image 183
JKirchartz Avatar answered Sep 19 '22 18:09

JKirchartz


All I need is a brief PHP example of how to open a TXT file on the server, read it sequentially, display the data on the screen, and close the file, as in this pseudo-code:

echo file_get_contents('/path/to/file.txt');

Yes that brief, see file_get_contents, you normally don't need a loop:

$file = new SPLFileObject('/path/to/file.txt');
foreach($file as $line) {
    echo $line;
}
like image 41
hakre Avatar answered Sep 21 '22 18:09

hakre