Forum Moderators: coopster
$fp = fopen("myfile.txt", "r");
$temp = fread ($fp, filesize ("myfile.txt"));
fclose ($fp);
$datalines = explode ("\n", $temp);
However the elements of the $datalines array are empty, i.e. they do not contain the lines of the file.
Before doing this I tried:
$datalines = file ("myfile.txt");
getting an array with 3500 empty elements. What can I do? Needless to say, I'm quite frustrated. In any major programming language you can declare an array of a predefined size, it exists and it is able to store what it has to store.
I might add that in a second step I want to sort the lines of this array using a sort function (will probably use a simple bubblesort). So I really need the array data structure otherwise the sorting gets quite difficult.
By the way, my provider has PHP 4.2.
not if you mess it up ;)
Have you verified the data to ensure that everything is OK with text file? When you wrote these random lines of text to the txt did you write a newline char to the end of each line? It seems to know how many lines are there but doesn't get the data into it.
Have you tried a preg_split instead to see if that works?
$datalines = file ("myfile.txt");
foreach ($datalines as $zz) {
echo $zz . "<br>"; }
for ($i = 0; $i <= 10; $i++) {
echo $i . ": " . $datalines[i] . "<br>"; }
The foreach thing correctly echoed all lines of the file, but the for loop just echoed empty lines.
It seems to be an indexing or key problem. What key do I need to use to access the elements of the array? An integer from 0 to number of elements-1 or what else?
All you have to do is use fopen [php.net] to open the text file, use fread to read the contents into a var and then echo the var between the textarea tags. Then do what you need to when it's submitted.