Forum Moderators: coopster

Message Too Old, No Replies

How to dump file to variable?

         

kmonroe

9:05 pm on Jul 29, 2010 (gmt 0)

10+ Year Member



Where am I going wrong?
list.txt (note: this file changes)
---------------
dragon.jpg
fade.jpg
smudge.jpg
smudge1.jpg
splat.jpg
sticky.jpg
sticky1.jpg
off.gif
on.gif
warning.png
--------------



<?
$file_handle = fopen("list.txt", "rb");
while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
$myArray = array($line_of_text);

}

list($var1, $var2, $var3) = array_values($parts); #how do i make this able to change as the list.txt changes?

echo $var1;
echo $var2;
echo $var3;

?>

Readie

9:28 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than using fopen, fread and fclose, I suggest using file_get_contents()

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
[uk3.php.net...]

Much simpler:

$var = file_get_contents('/path/to/file.txt');
echo $var;

Jotun

9:33 pm on Jul 29, 2010 (gmt 0)

10+ Year Member



This is how I would do it:

$file_handle = fopen("list.txt", "rb"); // Open the file.
$parts = array(); // Declare this variable outside of the loop so it's visible.
while ($parts[] = fgets($file_handle)) {} // Cycle through until the end of the file.
// Store the current line as the next element in $parts[]. You could keep your current conditional statement
// inside the while loop and move what I have down inside of it. But I prefer this method. It's more compact.


// Don't use seperate variables here to store each value. You said the list changes. That's what arrays are for.
// You probably want to cycle through the array and do something. We'll just print it back out with line numbers.
for ($i=0; $i < sizeof($parts); $i++){
echo "($i) $parts[$i]<br />";
}
fclose($file_handle); // Close the file.

Readie

9:40 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Jotun, welcome to Webmaster World.

Just a pointer:

for ($i=0; $i < sizeof($parts); $i++){

You're calling the sizeof() function on every iteration of the loop here, it's much less CPU intensive to declare a variable beforehand and reference that in the for() loop.

Matthew1980

9:55 pm on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Only one (main) nugget from me tonight:

Please try to use full tags:<?php ?>

As opposed to : <? ?>

Primarily because not all servers support the use of short tags in the php.ini file, and say you come to migrate from one server to another, this could potentially cause the script to fail, by using the full tags, there is much more cross server compatibility there - this will save on headaches later on.

And yes: file_get_contents() is a much better option to use rather than fopen, as it reads the entire file into a string that you can store in a variable :)

And WRT the contents changing, is there a pattern to the changes ie, is it a recurring time cycle, if so this could potentially be a 'cron job' task, just a thought there - I could be wrong ;)

Cheers,
MRb

kmonroe

11:18 pm on Jul 29, 2010 (gmt 0)

10+ Year Member



How do I get
ex.
$a1=dragon.jpg
$a2=fade.jpg
$a3=smudge.jpg
etc...
I don't want a list.. I am trying to use it in a form...
I need to be able to echo it