Fredashay
Fredashay

Reputation: 1033

Can I read a .TXT file with 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!

Upvotes: 12

Views: 67872

Answers (6)

hakre
hakre

Reputation: 197658

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;
}

Upvotes: 9

JKirchartz
JKirchartz

Reputation: 18022

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);
}
?>

Upvotes: 28

Zed
Zed

Reputation: 595

You want to read line by line? Use fgets.

$handle = @fopen("myfile.txt", "r");
if ($handle) {
    while (($content = fgets($handle, 4096)) !== false) {
        //echo $content;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

Upvotes: 0

Harry
Harry

Reputation: 344

Why you not read php documentation about fopen

 $file = fopen("source/file.txt","r");
  if(!file)
    {
      echo("ERROR:cant open file");
    }
    else
    {
      $buff = fread ($file,filesize("source/file.txt"));
      print $buff;
    }

Upvotes: 2

Zed
Zed

Reputation: 595

file_get_contents does all that for you and returns the text file in a string :)

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

Well, since you're asking about resources on the subject, there's a whole book on it in the PHP.net docs.

A basic example:

<?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);
?>

Upvotes: 3

Related Questions