Page 1 of 1
Problems w/ sending data to file using fwrite()
Posted: Wed Aug 21, 2002 5:40 pm
by m3mn0n
Code: Select all
<?
function WriteToFile ($URL, $Description) {
//Function WriteToFile takes two arguments--URL and Description--which will be written to an external file
$TheFile = "data.txt";
$Open = fopen ($TheFile, "a");
if ($Open) {
fwrite ($Open,
"$URL\n $Description\n");
fclose ($Open);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
}
//End of WriteToFile Function.
?>
<html>
<head>
<title>URL Submission</title>
<body>
<?php
/* this page receives and handles the data generated by "form.html" */
$Pattern = "http://)?(ї^ї:space:]]+)(її:alnum:]\.,-_?/&=])";
if (eregi($Pattern, $Arrayї"URL"])) {
$Replace = "<a href="http://\\2\\3" target="_new">\\2\3\</a>";
$Arrayї"URL"] = eregi_replace($Pattern, $Replace, $Arrayї"URL"]);
$CallFunction = WriteToFile ($Arrayї"URL"], $Arrayї"Description"]);
if ($CallFunction) {
print ("Your submission--$ArrayїURL]--has been received!<BR>\n");
} else {
print ("Your submission was not processed due to a system error!<br>\n");
}
} else {
print ("Print enter a valid Web address!\n");
}
?>
</body>
</html>
It does send some data to the text file but it doesn't send it in a proper form...
NotePad wrote:
<a href="http://m\3" target="_new">m\</a> MSN
It is supose to send the URL to the line with the discription on another..
I've tried everything and this still is not working correctly.
Posted: Wed Aug 21, 2002 10:39 pm
by nielsene
First, $Pattern seems to be missing an opening '(' which will cause all your numbered memories to be off by one (so only \1 and \2 are defined). Also I think you left off a '*' at the end of your pattern. (That's why its only getting a single letter "m" for the match.)
Next the second use of the third pattern in Replace needs an extra '''. (That's what's generating the little box, "\3" is an unprintable control character)
Your actual no newline problem is, I suspect, due to Windows wanting \r\n and not just \n.
Posted: Wed Aug 21, 2002 10:41 pm
by DesignerSMS
This does output the text with the newline character at the end of each section.
I would guess that you are viewing this file on a windows based system (notepad?).
Windows delimits each line with two characters, the carriage return and newline characters.
If you want this file to look like in Windows:
Code: Select all
http://www.designersms.com
Yes, I know, a blatent plug.
change your code on the fwrite(...) line to:
Code: Select all
fwrite($Open, "$URL\r\n$Description\r\n");
Also, a little tip - the most common standard for variable names in PHP, C, C++, Java, VisualBasic, etc is to start variable and function names with lower case and then capitalise each word after that so a simple, one-word variable would look like
$file and a multiple-word variable would look like
$theFileLocation.
Hope this helps.
Posted: Thu Aug 22, 2002 3:44 am
by m3mn0n
thank you guys ALOT!
looks like the author of this book made a lil boo boo

, his screen shot was a Mac OS so i'm guessing he never tested this w/ windows.