[SOLVED] fwrite Overwriting Files

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
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

[SOLVED] fwrite Overwriting Files

Post by kaYak »

I am working with fwrite to write to a file. It must be overwriting the file. Does this code look right?

It grabs the file's contents to display in a text area:

Code: Select all

$template = $_POST['s_temp'] . "_temp.tpl";

$template_file = file_get_contents("/home/research/public_html/smarty/site/templates/$template");
After the person edits the file it is submitted and then grabbed and saved:

Code: Select all

$new_temp_content = $_POST['temp_content'];
$new_temp_content = stripslashes($new_temp_content);
$template_file = $_POST['tf'];

$handle = fopen("/home/research/public_html/smarty/site/templates/$template_file", 'w');

fwrite($handle, $new_temp_content);

fclose($handle);
Could someone tel me if there is something wrong here?
Last edited by kaYak on Mon Sep 27, 2004 5:12 pm, edited 1 time in total.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

What exactly is your problem?. Are you getting errors perhaps?.
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

Post by kaYak »

Joe wrote:What exactly is your problem?. Are you getting errors perhaps?.
No errors, it's strange. I copied it over from another site and I just changed a few paths, and that's all, so I don't really understand why it's not working.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Sorry about the questions but I am just trying to get to the bottom of the problem. When it fails to work what are you resulted in?. A blank screen perhaps?.
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

Post by kaYak »

Joe wrote:Sorry about the questions but I am just trying to get to the bottom of the problem. When it fails to work what are you resulted in?. A blank screen perhaps?.
I don't mind the questions at all. I am not resulted in a blank screen. All of this is for a CMS, and the file writing should write to a template file. And I have it so it will display a page with the smarty template system which is does successfully, but that isn't the problem. PHP doesn't show any errors or anything, it follows through as though it was working.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try:

Code: Select all

$handle = fopen("/home/research/public_html/smarty/site/templates/".$template_file, 'w+');
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

Post by kaYak »

Joe wrote:Try:

Code: Select all

$handle = fopen("/home/research/public_html/smarty/site/templates/".$template_file, 'w+');
No, that doesn't work :( If you would still like to try and help please do, if not thank you.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

The line:

$template_file = $_POST['tf'];

Are you sure the form is actually POSTING and that the html form has an element name tf?.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Oh and the file mode should be w+ not w.
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

Post by kaYak »

Joe wrote:The line:

$template_file = $_POST['tf'];

Are you sure the form is actually POSTING and that the html form has an element name tf?.
I fixed it, :). For some reason the form input name was different for the text area's template information so php was grabbing nothing. Thanks for your help! :D
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

No problem.

8)
Post Reply