Read a line in file txt in PHP [duplicate] - php

This question already has answers here:
PHP: Read Specific Line From File
(13 answers)
Closed 7 years ago.
I want read a line in file text. EX line 5. Any body can help me????
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
echo fgets($fp);
}
fclose($fp);
Thanks for read

You can use the fgets() function to read the file line by line:
<?php
$handle = fopen("test.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line.'<br/>';
}
fclose($handle);
} else {
// error opening the file.
}
?>

$myFile = "text.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2
PHP - Reads entire file into an array

Just put an incremental counter in your loop, only echo if that counter matches your requred line number, then you can simply break out of the loop
$required = 5;
$line = 1;
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
if ($line == $required) {
echo fgets($fp);
break;
}
++$line;
}
fclose($fp);

Related

Read specific line from csv file on php [duplicate]

This question already has answers here:
PHP dynamically create CSV: Skip the first line of a CSV file
(8 answers)
Closed 3 years ago.
How do I get the second line from this CSV file?
<?php
$file = file('list.csv');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
?>
You can use an iterator to apply condition
$file = file('list.csv');
$i = 0;
while (($line = fgetcsv($file)) !== FALSE) {
if($i == 1){
print_r($line);
break;
}
$i++
}
Firstly file() reads the file into an array, so you don't need that - just open it using fopen() (and don't forget to close it), then if you only want the second line, then just call fgetcsv() twice...
$file = fopen('list.csv', r);
$line = '';
if (fgetcsv($file) !== FALSE) {
$line=fgetcsv($file);
}
fclose($file);

how to delete first line from a text file in php [duplicate]

This question already has answers here:
Is this the most efficient way to get and remove first line in file?
(12 answers)
Closed 6 years ago.
I try to make a simple php to read and delete first line from a text file.
So far I got the code:
<?php
$myfile = fopen("text.txt", "r") or die("Unable to open file!");
$ch=1;
while(!feof($myfile)) {
$dataline= fgets($myfile);
$listes = explode("\n",file_get_contents("text.txt"));
$poc = $listes[0];
if($ch == 2){
$gol = str_replace(' ', ' ', $dataline)."\n";
$fh = fopen("newtext.txt",'a');
fputs($fh, $gol);
fclose($fh);
chmod("newtext.txt", 0777);
}
$ch = 2;
}
unlink("text.txt");
copy("newtext.txt", "text.txt");
chmod("text.txt", 0777);
unlink("newtext.txt");
fclose($myfile);
echo $poc;
flush();
?>
The code is working only for the first two lines in text.txt but when it should read the third line the code stop working. Any advice please.
$handle = fopen("file", "r");
$first = fgets($handle,2048); #get first line.
$outfile="temp";
$o = fopen($outfile,"w");
while (!feof($handle)) {
$buffer = fgets($handle,2048);
fwrite($o,$buffer);
}
fclose($handle);
fclose($o);
rename($outfile,$file);
credits to ghostdog74 for this solution link

get the first 3 lines of a text file in php [duplicate]

This question already has answers here:
How to read a large file line by line?
(14 answers)
Closed 8 years ago.
I am developing a website in PHP, and I must include in the index the first 3 lines of a text file in PHP. How can I do that?
<?php
$file = file_get_contents("text.txt");
//echo the first 3 lines, but it's wrong
echo $file;
?>
Even more simple:
<?php
$file_data = array_slice(file('file.txt'), 0, 3);
print_r($file_data);
Open the file, read lines, close the file:
// Open the file for reading
$file = 'file.txt';
$fh = fopen($file, 'rb');
// Handle failure
if ($fh === false) {
die('Could not open file: '.$file);
}
// Loop 3 times
for ($i = 0; $i < 3; $i++) {
// Read a line
$line = fgets($fh);
// If a line was read then output it, otherwise
// show an error
if ($line !== false) {
echo $line;
} else {
die('An error occurred while reading from file: '.$file);
}
}
// Close the file handle; when you are done using a
// resource you should always close it immediately
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
The file() function returns the lines of a file as an array. Unless the file is huge (multiple megabytes), you can then use array_slice to get the first 3 elements of this:
$lines = file('file.txt');
$first3 = array_slice($lines, 0, 3);
echo implode('', $first3);

Get only one line from a web page

Hi, I have this file test1.php and in the other file test.php I have this
php code running:
<?php
$file = "http://inviatapenet.gethost.ro/sop/test1.php";
$line = '0';
if($f = fopen($file, 'r')){
$line = fgets($f); // read until first newline
fclose($f);
}
echo $line;
?>
The idea is to get just the second line of the web page test1.php.
Second Line
I've tried to change the $line = '2'; but no affect, it just displays the first line.
I need Help.
You can use file which reads a file into an array, you can then grab whichever line you want by using the index you want.
For example:
data.txt:
line one
line two
line three
line four
PHP code:
$file = file('data.txt');
echo $file[1]; // echo line number 2, remember arrays start at 0!
Updated PHP code for new versions (5.4):
echo file('data.txt')[1];
This should work. Obviously, change only the value of $linetofetch:
<?php
// Write here the number of the line you want to fetch.
$linetofetch = 2;
$file = "http://inviatapenet.gethost.ro/sop/test1.php";
$currentline = 1;
if($f = fopen($file, 'r')){
while ($currentline <= $linetofetch) {
$line = fgets($f); // read until first newline
$currentline++;
}
fclose($f);
}
echo $line;
?>

How to remove a line from text file using php without leaving an empty line [duplicate]

This question already has answers here:
How to delete a line from the file with php?
(10 answers)
Closed last year.
currently I am able to remove a specific line from text file using php. However, after removing that line, there will be an empty line left behind.
Is there anyway for me to remove that empty line so that the lines behind can move up?
Thank you for your help.
$DELETE = "the_line_you_want_to_delete";
$data = file("./foo.txt");
$out = array();
foreach($data as $line) {
if(trim($line) != $DELETE) {
$out[] = $line;
}
}
$fp = fopen("./foo.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line) {
fwrite($fp, $line);
}
flock($fp, LOCK_UN);
fclose($fp);
this will just look over every line and if it not what you want to delete, it gets pushed to an array that will get written back to the file.
Really? I find this muuuuch easier, only one line of code:
file_put_contents($filename, str_replace($line . "\r\n", "", file_get_contents($filename)));
You can improve this by setting conditions to predict errors.
<?PHP
$file = "a.txt";
$line = 3-1;
$array = file($file);
unset($array[$line]);
$fp = fopen($file, 'w+');
foreach($array as $line)
fwrite($fp,$line);
fclose($fp);
?>

Categories