How to Read a .Txt File with PHP

Read a plain text file with php

<?php

$fh = fopen('filename.txt','r');
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
// echo($line);
}
fclose($fh);
?>

This will give you a line by line read.. read the notes at php.net/fgets regarding the end of line issues with Macs.

How can I read .txt file from my server [PHP]?

Since you are opening from a remote host you can't open for read/write, just read. Try this instead:

$fp = fopen($filename, "r");

If you are trying to open a file on the same server as the PHP script then do not use the "http://..." path but instead the local file path and then you can open it for read/write. In the case of your script it does not appear you need write access so "r" should be sufficient.

To access without http just use the path to the file:

$filename = '/path/to/file/text.txt';
$fp = fopen($filename, "r+");

Or if you want a relative path from the script itself I prefer:

$filename = dirname(__FILE__) . '/../some/relative/path/text.txt';

Reading from a Text File in PHP

PHP variables are case sensitive. $mapFile !== $mapfile.

$mapFile = fopen('config/map.txt', 'r'); // don't need `or die()`
while ($line = fread($mapFile, filesize('config/map.txt')))
{
echo $line;
}

PHP cant read txt file

Is the txt file very larger? Perhaps PHP is choking on the file size? Check upload_max_filesize and post_max_size php config settings - I've seen situations where the file exceeds post_max_size did not return any errors, similar to your situation.

How to read text-file in php and send the results to other html file

if the data file and the created one is on the same server, this should work

<?php

$data_read = file_get_contents("data.txt");

// do stuff

$success = (file_put_contents("a_body.html",$data_read)?true:false);

?>

instead of file_get_contents() u also can use the file() function to get the data.txt line by line as an array. Or use fopen() - fread() - fclose() if needed.

PHP read TXT file from a special character

A nice short answer is to just read the whole file in and extract the piece of text from PHP_EOL.'@'.PHP_EOL to make sure that it finds an @ on a line on it's own. Then trim off the PHP_EOL and the @ off the start. Finally to split to an array, use explode of the remainder...

$text = file_get_contents("a.txt");
$end = ltrim(strstr($text, PHP_EOL."@".PHP_EOL ), PHP_EOL."@");
$out = explode(PHP_EOL, $end);
print_r($out);

If you had a file with...

text1
text2
text3
@
new text1
new text2
new text3
--
aaa

You can use strstr() again to pick out the content from the @ and then before the -- line...

$text = file_get_contents("a.txt");
$end = ltrim(strstr($text, PHP_EOL."@".PHP_EOL ), PHP_EOL."@");
$end = strstr($end, PHP_EOL."--".PHP_EOL, true);
$out = explode(PHP_EOL, $end);
print_r($out);

will output

Array
(
[0] => new text1
[1] => new text2
[2] => new text3
)

Read a txt file in Storage

You can use the comand feof (find end of file), so you can do anything you want to inside the loop.

$fp = fopen(Storage::path('public/' . $file_path->path_to_file), "r");
while(!feof($fp)){
$line = fgets($fp);
print_r($line."\n");
}
fclose($fp);


Related Topics



Leave a reply



Submit