Page 1 of 1

Generating files

Posted: Wed Oct 25, 2006 5:22 am
by slimt_slimt
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi, 

i have the following code, which doesn't have the right output. Is because of !feof

Code: Select all

<?php
$file = "readfrom.txt";
$fh = fopen($file, 'r');
while (!feof($fh))
{
$line = fgets($fh, filesize($file));
echo $line;
echo "<br>";
$fd = fopen($line, 'x')or die ("E1");
fwrite($fd, "plain text here") or die ("E2");
fclose($fd);
fclose($fh);
}
?>
readfrom.txt

Code: Select all

name1
name2
name3
name4
and idea is simple. To read from readfrom.txt the names of the files and generate files with the content "plain text here":

Code: Select all

name1.txt
name2.txt
name3.txt
name4.txt

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 25, 2006 5:58 am
by choppsta
You are executing fclose($fh) after the first iteration of the while loop. You need to move it outside the loop.

Try this

Posted: Wed Oct 25, 2006 5:59 am
by gunman
Try this copy from php manual with a little modification

Code: Select all

$lines = file ('http://www.example.com/'); 

// Loop through our array, show html source as html source; and line numbers too. 
foreach ($lines as $line_num => $line) { 
   $h = fopen($line, "a");
   fwrite($h, "Plain Text");
   fclose($h);
}

Posted: Wed Oct 25, 2006 6:02 am
by Ward
You're closing the read file before it's finished reading. Look where your fclose($fh) is.

Code: Select all

$source_file = fopen("readfrom.txt","r");
while (!feof($source_file))
{
    $line = fgets($source_file,1024);
    $new_file_name = $line.".txt";
    $new_file = fopen($new_file_name,"w");
    fwrite($new_file, "plain text here");
    fclose($new_file);
}
fclose($source_file);

Posted: Wed Oct 25, 2006 6:11 am
by slimt_slimt
Ward: thank you for your reply.
there is a slight problem. the script generates only the last line from "readfrom.txt" file and therefore the outcome is only one file, instead of all the files.

Posted: Wed Oct 25, 2006 6:44 am
by Ward
could be because of the newlines in the filename strings. try:

Code: Select all

$source_file = fopen("readfrom.txt","r");
while (!feof($source_file))
{
    $line = fgets($source_file,1024);
    $new_file_name = trim($line).".txt";
    $new_file = fopen($new_file_name,"w");
    fwrite($new_file, "plain text here");
    fclose($new_file);
}
fclose($source_file);

[SOLVED]

Posted: Wed Oct 25, 2006 7:06 am
by slimt_slimt
Ward: thank you.

"trim" was the only missing thing.....
grrr....but it's solved now!