Page 1 of 1

File write problem

Posted: Sat Jul 14, 2007 4:23 pm
by infl8
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi everyone. I'm pretty new to php so bear with me, I'm sure this is a simple fix.

I wrote a really simple script that uses some form data to write a file. It works just fine on my computer with Apache and PHP 5, but when I upload it to a PHP enabled server it doesn't work. I'm pretty sure it's because I didn't put anything in the script to log into the the web space (or something like that). How would I go about fixing this?

This is the error message I'm getting.

Warning: fopen(track_status.php) [function.fopen]: failed to open stream: Permission denied in (directory which I'm not going to show here)/track_status_update.php on line 62
can't open file

Here is my code..

******************
[syntax="html"]<form action="track_status_update.php" method="post">
	Title<br />
	<input name="header" type="text" value="" size="30"><br /><br />

	Update<br />
	<textarea name="message" rows="3" cols="40"></textarea><br />

	<input type ="submit" name="submit" value="&nbsp;&nbsp;&nbsp;Update Track Status Note&nbsp;&nbsp;&nbsp;"><br />
	<input type ="reset" name="reset" value="&nbsp;&nbsp;&nbsp;Reset Form&nbsp;&nbsp;&nbsp;">
</form>
******************

When submitted that loads "track_status_update.php" with this code in it


******************[/syntax]

Code: Select all

<?php

	$header1 = "<h1>";
	$header2 = "</h1>";

	$myFile = "track_status.php";
	$fh = fopen($myFile, 'w') or die("can't open file");

	$stringData = "<h2>&nbsp;</h2>";
	fwrite($fh, $stringData);

	$stringData = $header1 . $_POST["header"] . $header2;
	fwrite($fh, $stringData);

	$stringData = $_POST["message"];
	fwrite($fh, $stringData);

	$stringData = "<br /><br />";
	fwrite($fh, $stringData);

	fclose($fh);
?>
******************

And that's where it stops working. It should just write to "track_status.php." But the fopen function is denied.

Thanks everyone,
infl8


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 15, 2007 6:17 am
by superdezign
What are the permissions of track_status.php and it's parent directory?

Posted: Sun Jul 15, 2007 9:30 am
by volka
please try

Code: Select all

<?php
$header1 = "<h1>";
$header2 = "</h1>";

$myFile = "track_status.php";

echo "<pre>Debug:\n";
echo 'realpath: ', realpath('.'), "\n";
echo 'who am i: ', system('whoami'), "\n"; // see http://unixhelp.ed.ac.uk/CGI/man-cgi?whoami
echo 'ls -lad .: ', system('ls -lad .'), "\n";
echo 'ls -lad myfile: ', system('ls -lad '.$myFile), "\n";
echo "<pre>\n";

$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = "<h2>&nbsp;</h2>";
fwrite($fh, $stringData);

$stringData = $header1 . $_POST["header"] . $header2;
fwrite($fh, $stringData);

$stringData = $_POST["message"];
fwrite($fh, $stringData);

$stringData = "<br /><br />";
fwrite($fh, $stringData);

fclose($fh);
?>
it should print what superdezign is asking for.