Log in

View Full Version : File Creation Script Help



Titan85
12-08-2006, 09:33 PM
Hello, recently I have been working on a simple script that allows you to input data into a form and then creates a .html page with that data on it. I think i have it mostly working, but for some reason there is an error while creating the page. Here is my code:

Form Page (game_record.php):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="game_records" name="game_recorder" method="post" action="game_process.php">
Page Title:
<br />
<input type="title" name="textfield" />
<br />
<br />
Filename:
<br />
<input type="filename" name="textfield" />
<br />
<br />
Team Names:
<br />
<input name="team1" type="text" />
<br />
<input name="team2" type="text" />
<br />
<br />
Winning Team:
<br />
<input name="win" type="text" />
<br />
<br />
Score:
<br />
<input name="score" type="text" />
<br />
<br />
Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />
<br />
<br />
<input type="submit" />
</form>
</body>
</html>

Data Process Page (game_process.php):
<?
require("includes.php");

$filename = $_REQUEST['filename']; //Get filename

//Add title
$title = stripslashes($_POST['title']);

//Add team1
$team1 = stripslashes($_POST['team1']);

//Add team2
$team2 = stripslashes($_POST['team2']);

//Add winning team
$win = stripslashes($_POST['win']);

//Add score
$score = stripslashes($_POST['score']);

//Add game 1
$game1 = stripslashes($_POST['game1']);

//Add game 2
$game2 = stripslashes($_POST['game2']);

//Add game 3
if($game3 = stripslashes($_POST['game3'])) {

$add = '

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>'.$title.'</title>
</head>

<body>
</b>Game Stats for game with '.$team1.' vs '.$team2.'</b>
<br />
<br />
Match Score: '.$score.'
<br />
Winning Team: '.$win.'
<br />
Game Links:
<ul>
<li><a href="'.$game1.'">'.$game1.'</a></li>
<li><a href="'.$game2.'">'.$game1.'</a></li>
<li><a href="'.$game3.'">'.$game3.'</a></li>
</ul>
';

$game = $add;

$file = @fopen($filename, "w+");
@fwrite($file, $game);
@fclose($file);
$message = $success;
}
else {
$message = $add_error;
}
echo'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="refresh" content="5;url=game_record.php">
<title>Status</title>
</head>

<body>
'.$message.'
</body>
</html>
';
?>I hope that is enough to find the problem, if not just post back saying what you need and I will get that to you ASAP. Thanks

thetestingsite
12-09-2006, 12:56 AM
Whats the error that you get, from looking at the above code, I cannot find anything wrong, but if we had an error message, maybe we could pinpoint the problem. (or even a link to the script would be helpful).

Titan85
12-09-2006, 06:25 AM
Ok, the only error I get is that off the message I defined as the error message if it failed to create the file. The link to the test is: http://www.echo-designes.com/testing/game_recorder/game_record.php . Hope you can find something :)

thetestingsite
12-09-2006, 05:12 PM
Make sure that the directory that the file is being written to has writable permissions (777). If it does, try taking the "@" sign of the front of fopen, fwrite, etc to see if you get a PHP error. Hope this helps.

Titan85
12-10-2006, 04:11 AM
Make sure that the directory that the file is being written to has writable permissions (777). If it does, try taking the "@" sign of the front of fopen, fwrite, etc to see if you get a PHP error. Hope this helps.I made sure the folder had writable permission and had already taken out the @ sign, but still no actual PHP error, just the one i set. Anything else to do?

thetestingsite
12-10-2006, 04:44 AM
The problem with the code is the HTML page, look at the following and below is the corrections.



Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />


correction:



Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game2" type="text" />
<br />
<input name="game3" type="text" />


After fixing that, it gives the PHP errors:



Warning: fwrite(): supplied argument is not a valid stream resource in C:\wamp\www\test\game_process.php on line 58

Warning: fclose(): supplied argument is not a valid stream resource in C:\wamp\www\test\game_process.php on line 59
success


I'm currently looking for the reason, and a fix for it. As soon as I get it fixed, I will post the corrected code here and explain the problems.

Titan85
12-10-2006, 04:47 AM
The problem with the code is the HTML page, look at the following and below is the corrections.



Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />
<br />
<input name="game1" type="text" />


correction:



Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game2" type="text" />
<br />
<input name="game3" type="text" />


After fixing that, it gives the PHP errors:



I'm currently looking for the reason, and a fix for it. As soon as I get it fixed, I will post the corrected code here and explain the problems.Thanks, I await an idea on fixing it :)

thetestingsite
12-10-2006, 04:52 AM
I have found the reason why it is not working. And it is all in the HTML code.

Corrections to your original code are as follows (you will see new additions/corrections in red):



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="game_recorder" method="post" action="game_process.php">
Page Title:
<br />
<input type="text" name="title" />
<br />
<br />
Filename:
<br />
<input type="text" name="filename" />
<br />
<br />
Team Names:
<br />
<input name="team1" type="text" />
<br />
<input name="team2" type="text" />
<br />
<br />
Winning Team:
<br />
<input name="win" type="text" />
<br />
<br />
Score:
<br />
<input name="score" type="text" />
<br />
<br />
Game Links:
<br />
<input name="game1" type="text" />
<br />
<input name="game2" type="text" />
<br />
<input name="game3" type="text" />
<br />
<br />
<input type="submit" />
</form>
</body>
</html>


Sorry it took so long to figure it out, I really need to start paying attention to the HTML first before looking at the PHP. I have tested it and it works perfectly now. Let me know if you still have problems.

Titan85
12-10-2006, 05:11 AM
Thanks a lot man, i really appreciate it and it works great. Darn little errors : / . O, I have another kinda off topic question. Say I want have the data added to two pages, like one page has the full info, but the other one gets a link to the page added, how would I do that?