Problems w/ sending data to file using fwrite()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Problems w/ sending data to file using fwrite()

Post by m3mn0n »

Code: Select all

<?
function WriteToFile ($URL, $Description) &#123;
//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) &#123;
		fwrite ($Open,
	"$URL\n $Description\n");
	fclose ($Open);
	$Worked = TRUE;
	&#125; else &#123;
	   	$Worked = FALSE;
	&#125;
&#125; 
//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://)?(&#1111;^&#1111;:space:]]+)(&#1111;&#1111;:alnum:]\.,-_?/&=])";
if (eregi($Pattern, $Array&#1111;"URL"])) &#123;
		$Replace = "<a href="http://\\2\\3" target="_new">\\2\3\</a>";
		$Array&#1111;"URL"] = eregi_replace($Pattern, $Replace, $Array&#1111;"URL"]);
		$CallFunction = WriteToFile ($Array&#1111;"URL"], $Array&#1111;"Description"]);
		if ($CallFunction) &#123;
		print ("Your submission--$Array&#1111;URL]--has been received!<BR>\n");
	&#125; else &#123;
		print ("Your submission was not processed due to a system error!<br>\n");
		&#125;
	&#125; else &#123;
		print ("Print enter a valid Web address!\n");
&#125;
?>
</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
8O :?: :!:

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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
DesignerSMS
Forum Newbie
Posts: 17
Joined: Tue Aug 06, 2002 12:16 am
Location: Gold Coast, Australia

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

thank you guys ALOT! :D

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.
Post Reply