How would I write the php to send the results to a text file and not display on the webpage?
so instead of this echo $v1.$v2.$v3.$v4."<br />";
could I send “$v1.$v2.$v3.$v4.” to “mytextfile.txt”?
How would I write the php to send the results to a text file and not display on the webpage?
so instead of this echo $v1.$v2.$v3.$v4."<br />";
could I send “$v1.$v2.$v3.$v4.” to “mytextfile.txt”?
Yes, you’d just need to use the [fphp]fopen[/fphp],[fphp]fwrite[/fphp] and [fphp]fclose[/fphp] series of functions. A quick example would be:
$fp = fopen('myfile.txt', 'w');
fwrite($fp, "$v1.$v2.$v3,$v4\
");
fclose($fp);
This would open a file, myfile.txt in the caller script’s working directory in write mode, write a string to it, then close the file. Note that you’d need to make sure that wherever you plan to write files to in PHP have the proper write permissions set.
If you are using PHP 5 file_put_contents is easier and faster.
Down with PHP 4!! It’s day is done. Long live PHP 5 (well, until 6 is released anyway )
I tried chris’s script, it created the text file but it only had one line of this:
z.z.z
this is my full script:
<?
$alpha = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
$x = explode(" ",$alpha);
foreach($x as $v1)
{
foreach($x as $v2)
{
foreach($x as $v3)
{
// echo $v1."-".$v2.$v3."<br />";
//echo $v1.$v2.$v3.$v4."<br />";
$fp = fopen('myfile.txt', 'w'); fwrite($fp, "$v1.$v2.$v3\
"); fclose($fp);
}
}
}
?>
when this script runs with this instead
echo $v1.$v2.$v3."<br />";
then the output looks like this
aaa
aab
aac
etc etc …
Michael, I do have php5 but I’m unsure how to write this for mine
i edited & updated that last post
You have this code inside your loop:
$fp = fopen('myfile.txt', 'w'); fwrite($fp, "$v1.$v2.$v3\
"); fclose($fp);
Which means that on every iteration of the loop the file will be opened, truncated (‘emptied’) then written to, so you’ll only ever end up with the last iteration written to the file.
To fix your code just put the file opening/closing outside of the loop:
$alpha = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
$x = explode(" ",$alpha);
$fp = fopen('myfile.txt', 'w');
foreach($x as $v1)
{
foreach($x as $v2)
{
foreach($x as $v3)
{
fwrite($fp, "$v1.$v2.$v3\
");
}
}
}
fclose($fp);
Alternatively you can also do:
$output = array();
for($x = 'aaa'; $x != 'aaaa'; $x++)
{
$output[] = substr(chunk_split($x, 1, '.'), 0, -1);
}
file_put_contents('myfile.txt', join("\
", $output));
Just set up the entire content string and if(!file_put_contents($file, $content)) { // catch error }