/    Sign up×
Community /Pin to ProfileBookmark

Reading File Line by Line

Hello,

There is no problem here

[CODE]
$lines = ‘folder/file.txt’;

if(!empty($lines)){
foreach($lines as $line){

// Other codes
$newarray[] = $dataline;

} // foreach($lines as $line){
} // if(!empty($lines)){
[/CODE]

This is the question

$lines = array(“folder/file.txt”, “folder/file2.txt”, “folder/file3.txt”);

Read the files in array?

Thank you in advance

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 27.2016 — The non-commented code from above is:
[code=php]
$lines = 'folder/file.txt';
if(!empty($lines))
{
foreach($lines as $line)
{
$newarray[] = $dataline;
}
[/code]


You say there is no problem here. If posting incomplete code is not to be considered a problem, that's a true statement. But then your code goes on to treat the variable you created as an array. Hmmm... How did $lines become an array? And even supposing that there is something going on here that I have yet to learn about, where the h... did the $dataline variable come from?

I'm hoping that

a) you didn't write this code

b) and that you really want to learn how to write code.

As for your question, sure one can use your array statement to help read all the files whose names are in that array. Just not with ANY of the code you posted above.
Copy linkTweet thisAlerts:
@gecekuleauthorDec 27.2016 — Thank you very much for your answers

Question:

$lines = array("folder/file.txt", "folder/file2.txt", "folder/file3.txt");

How to use this array?


[CODE]

$lines = array("folder/file.txt", "folder/file2.txt", "folder/file3.txt");

if(!empty($lines))
{

foreach($lines as $line)
{

$newarray[] = $dataline;
}[/CODE]
Copy linkTweet thisAlerts:
@ginerjmDec 27.2016 — Do you have a definition of the 'empty()' function that you could share with me? I'm confused as to what you are using it for. That is because the PHP manual says it is used to check if something is EMPTY, not to READ a file with.
Copy linkTweet thisAlerts:
@gecekuleauthorDec 27.2016 — No no

I do not know english, I can not explain in detail

$lines = 'folder/file.txt';


No problem reading 1 file

[B]How to read too many files?[/B]

$lines = array("folder/file.txt", "folder/file2.txt", "folder/file3.txt");
Copy linkTweet thisAlerts:
@ginerjmDec 27.2016 — This line:
[code=php]
$lines = 'folder/file.txt';
[/code]

does NOT read a file.
Copy linkTweet thisAlerts:
@gecekuleauthorDec 27.2016 — No problem here, it works
[code=php]$file = fopen("file.txt",'r');
while(!feof($file)){

$row = fgets($file);
echo "$row <br />";
}
fclose($file);[/code]


file.txt

aaaaaaaa

bbbbbbbb

ccccccccc
[/QUOTE]


[B]Output[/B]

aaaaaaaa

bbbbbbbb

ccccccccc
[/QUOTE]

-------------------------------------------------------------

file.txt

aaaaaaaa

bbbbbbbb

ccccccccc
[/QUOTE]


file2.txt

dddddddd

eeeeeeee

ffffffffffff
[/QUOTE]


file3.txt

ggggggg

hhhhhhh

iiiiiiiiiiii
[/QUOTE]


This code is not working, where is the error?
[code=php]
$lines = array("file.txt", "file2.txt", "file3.txt");
$file = fopen($lines,'r');
while(!feof($file)){

$row = fgets($file);
echo "$row <br />";
}
fclose($file);[/code]
Copy linkTweet thisAlerts:
@ginerjmDec 27.2016 — Why don't you look at the manual and see how to use an array?

Hint - you can't open ALL the files at once!

Here is the official PHP Manual page on one command you could use:

http://php.net/manual/en/control-structures.foreach.php

This is the English version - the manual comes in other languages too!
Copy linkTweet thisAlerts:
@NogDogDec 27.2016 — I think maybe this is all you need?
[code=php]
$files = array("folder/file.txt", "folder/file2.txt", "folder/file3.txt");
foreach($files as $fileName) {
$fh = fopen($fileName, 'r');
if($fh == false) {
error_log("Could not open file '$fileName'"); // or whatever you want to do
continue; // if you want to go to next file in array
}
while(!feof($fh)){

$row = fgets($fh);
echo "$row <br />";
}
fclose($fh);
}
[/code]
Copy linkTweet thisAlerts:
@gecekuleauthorDec 28.2016 — I think maybe this is all you need?
[code=php]
$files = array("folder/file.txt", "folder/file2.txt", "folder/file3.txt");
foreach($files as $fileName) {
$fh = fopen($fileName, 'r');
if($fh == false) {
error_log("Could not open file '$fileName'"); // or whatever you want to do
continue; // if you want to go to next file in array
}
while(!feof($fh)){

$row = fgets($fh);
echo "$row <br />";
}
fclose($fh);
}
[/code]
[/QUOTE]


Yes yes yes,

Thank you very much
Copy linkTweet thisAlerts:
@gecekuleauthorDec 28.2016 — Why don't you look at the manual and see how to use an array?

Hint - you can't open ALL the files at once!

Here is the official PHP Manual page on one command you could use:

http://php.net/manual/en/control-structures.foreach.php

This is the English version - the manual comes in other languages too![/QUOTE]

I beginner,

I did not think it would be done with foreach.

Thank you for your help
Copy linkTweet thisAlerts:
@ginerjmDec 28.2016 — The real question is - Since NogDog wrote your code for you, did you LEARN anything? Or will you continue to copy code from anywhere and then ask us to fix it for you?
Copy linkTweet thisAlerts:
@gecekuleauthorDec 28.2016 — My job is not web design.

hobby is my work for myself
×

Success!

Help @gecekule spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...