...

View Full Version : Creating a txt file in PHP?



angst
09-06-2005, 08:08 PM
Hello,
I'm trying to create txt files in php,
I've found this page on the php manual:
http://ca3.php.net/fwrite

but this only write text to a txt file, and I need to know how to create the txt file, then write to it,

my fwrite code:



<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

if (is_writable($filename)) {

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>




any ideas how i can do this?

thanks in advance for your time!
-Ken

gsnedders
09-06-2005, 08:16 PM
fopen() creates the file if it doesn't exist.

angst
09-06-2005, 08:34 PM
ah, no it doesn't.

"x" must be included.
or in my case "x+"

like:

fopen($filename,"x+");


-Ken

angst
09-06-2005, 08:59 PM
one more question though,

I want to populate the txt file or html file rather with content from my data base,

so I'm wondering how this can be done?

example:





<?php
$dirname = 'test';

mkdir("/Program Files/xampp/htdocs/dating/$dirname", 0700);

$filename = "../$dirname/index.html";

if (is_writable($filename)) {
@unlink($filename);
}

fopen($filename,"x+");

if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');
$db_selected = mysql_select_db($SqlDb, $link) or die('Database name incorrect');
$result = mysql_query("SELECT * FROM subcategories WHERE cid=$_REQUEST[cat] ORDER BY subcategorie asc");

fwrite($handle, "
while ($row = mysql_fetch_array($result))
{
<font face=verdana size=1>[<a href='func.php?func=delsubcat&scid=<? echo $row[scid]; ?>&cat=<? echo $_REQUEST[cat]; ?>'>delete</a>] </font><font face=verdana size=2><? echo $row[subcategorie]; ?></font><br />
}
");

#echo "Success, generating html to files ($filename)";

fclose($handle);

header("Location: ../$dirname");
?>



something like that,
but i know what can't be done as is.
any ideas?

thanks again for your time!
-Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum