Page 1 of 1

fopen() not working...!

Posted: Mon Oct 10, 2005 10:29 pm
by sgt spike
Here's the deal right now. I've got one page (price.php) which I'm using for the password protection for the other page. Here's the code I use for that page.

Code: Select all

<html>
<head>
<title>Price List Editing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Please enter password to unlock:</p>
<form name="password" method="post" action="price_editor.php">
  <input type="text" name="password">
  <input name="submit" type="button" value="Submit">
</form>
<p>&nbsp;</p>
</body>
</html>
I have the second page (obviously price_editor.php) that I'm using to read and write to a price list. I'm using "|" as a tokenizer in the file to seperate entries. Here is the code I have:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
	$password = $_POST['password'];
	
	if ($password == "easy")
	{ 
	$filename = "mt102.txt";
	$file = fopen($filename, "a+");
	fseek($filename, 0);
	$filesize = filesize($filename);
	$text = fread($file, $filesize);
	$fclose = ($file);
	$count = 0;
	$tracker = 0;
	$arr = array();
	$arrcounter = 0;
	
	while ($count + 1 != $filesize)
	{
		$temp = substr($text, $count, $count + 1);
		if $temp = "|" then
		{
			$arr[$arrcounter] = substr($text, $tracker, $count + 1);
			$tracker = $count;
			$arrcounter = $arrcounter + 1;
		}
	}
	
	echo("Price for item 1 (current price is $arr[1])
	<br>Price for item 2 (current price is $arr[2])
	<br>Price for item 3 (current price is $arr[3])
	<br>Price for item 4 (current price is $arr[4])
	");
	echo("testing");
	}
	else
	{ echo("You are not authorized to view this page") ; }
?>
</body>
</html>
I can't get the page to load with opening the file. I've tried commenting out everything else in the php tags except the fopen() and fclose() function calls, but it still doesn't work with those. If I don't have file-related code running, then it works fine (for instance, echoing some text after validating the password). I have a text file called "mt102.txt" in the same directory as both of these pages as well. Any suggestions would be great, thanks!

Posted: Mon Oct 10, 2005 10:45 pm
by feyd
why not use file_get_contents() and explode() ?

Posted: Mon Oct 10, 2005 11:54 pm
by sgt spike
Wow, didn't even know those existed, but they're quite powerful. I'll try them and get back to this post if I still can't get the file to open. Thanks.