/    Sign up×
Community /Pin to ProfileBookmark

PHP Access Denied???

Ok, so here’s the problem. I’ve got a page with which I can edit the code of other pages. It’s not exactly user-friendly, and that’s the way I like it. But, I can’t get it to create files correctly. Code is below, followed by the errors I’m recieving.

[code=php]<?php
if ($_POST[‘filecontents’] != “”)
{
if (file_exists($_POST[‘filename’]) == FALSE)
{
touch($_POST[‘filename’]);
}
$handle = fopen($_POST[‘filename’], “w+”);
fwrite($handle, $_POST[‘filecontents’]);
fclose($handle);
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html>
<head>
<title>Create and or Edit Files</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
</head>
<body style=”text-align: center;”>
<?php
if ($_POST[‘filecontents’] != “”)
{
echo “You just saved a file!!! WHOOPIE!”;
}
?>
<form action=”filechanger.php” method=”post”><textarea rows=”20″ cols=”125″ name=”filecontents”><?php
if ($_GET[‘filename’] != “”)
{
$filename = $_GET[‘filename’];
$filecontents = file_get_contents($_GET[‘filename’]);
$safefilecontents = htmlspecialchars($filecontents);
echo $safefilecontents;
}
if ($_POST[‘filename’] != “”)
{
$filename = $_POST[‘filename’];
$filecontents = file_get_contents($_POST[‘filename’]);
$safefilecontents = htmlspecialchars($filecontents);
echo $safefilecontents;
}
?></textarea><input type=”hidden” value=”<?php echo $filename ?>” name=”filename” />
<?php
if ($filename == “”)
{
echo “n<br />You aren’t opening a file, so put the filename you want to save this text as right here:n<br /><input type=”text” size=”75″ />”;
}
?>
<br /><input type=”submit” value=”Save” /><input type=”reset” value=”Reset” /></form>
</body>
</html>[/code]

And that’s getting me this:

[quote=PHP]

Warning: touch() [function.touch]: Utime failed: Permission denied in C:Documents and Settings***My Documentsweb serverfilechanger.php on line 10

Warning: fwrite(): supplied argument is not a valid stream resource in C:Documents and Settings***My Documentsweb serverfilechanger.php on line 13

Warning: fclose(): supplied argument is not a valid stream resource in C:Documents and Settings***My Documentsweb serverfilechanger.php on line 14

[/quote]

I’m confused as to why it won’t work. I have other files in the save directory, creating files, by using fopen in x+ mode. Anyone think they know what I messed up? Thanks!

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@chazzySep 05.2006 — based on the description on php's site, touch doesn't work in windows.
Copy linkTweet thisAlerts:
@AmazingAntauthorSep 05.2006 — ok, based on that idea, i changed it to the code below, and it got rid of the error for touch. It still won't write the file however, and still gives the other two errors.
[code=php]<?php
if ($_POST['filecontents'] != "")
{
if (file_exists($_POST['filename']) == TRUE)
{
$handle = fopen($_POST['filename'], "w+");
}
if (file_exists($_POST['filename']) == FALSE)
{
$handle = fopen($_POST['filename'], "x+");
}
fwrite($handle, $_POST['filecontents']);
fclose($handle);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Create and or Edit Files</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="text-align: center;">
<?php
if ($_POST['filecontents'] != "")
{
echo "You just saved a file!!! WHOOPIE!";
}
?>
<form action="filechanger.php" method="post"><textarea rows="20" cols="125" name="filecontents"><?php
if ($_GET['filename'] != "")
{
$filename = $_GET['filename'];
$filecontents = file_get_contents($_GET['filename']);
$safefilecontents = htmlspecialchars($filecontents);
echo $safefilecontents;
}
if ($_POST['filename'] != "")
{
$filename = $_POST['filename'];
$filecontents = file_get_contents($_POST['filename']);
$safefilecontents = htmlspecialchars($filecontents);
echo $safefilecontents;
}
?></textarea><input type="hidden" value="<?php echo $filename ?>" name="filename" />
<?php
if ($filename == "")
{
echo "n<br />You aren't opening a file, so put the filename you want to save this text as right here:n<br /><input type="text" size="75" />";
}
?>
<br /><input type="submit" value="Save" /><input type="reset" value="Reset" /></form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@chazzySep 05.2006 — use if/else block. php parser is seeing it as no guaranteed definition of $handle for that block. if/else creates that guarantee
Copy linkTweet thisAlerts:
@AmazingAntauthorSep 05.2006 — [code=php]if (file_exists($_POST['filename']) == TRUE)
{
$handle = fopen($_POST['filename'], "w+");
}
else{
$handle = fopen($_POST['filename'], "x+");
}[/code]
And it still gives the same two errors, and still doesn't create a file.
Copy linkTweet thisAlerts:
@chazzySep 05.2006 — does the file really exist or not?

also, why are you using x+ at all? w+ should work in both cases...
Copy linkTweet thisAlerts:
@AmazingAntauthorSep 05.2006 — No it doesn't exist, which is why the point is for this php to create it. And i'm using x+ because the x+ mode is to:

Create and open for reading and writing; place the file pointer at the beginning of the file.[/quote]
And besides, even with just using w+, it still doesn't want to create the file.
Copy linkTweet thisAlerts:
@chesemonkylomaSep 05.2006 — i've gotten this error before, and in my case im pretty sure it was because i was opening a file in an absolute location, in your code it looks like you're not doing this, but if you are then change it.
Copy linkTweet thisAlerts:
@AmazingAntauthorSep 05.2006 — Well, chesemonkyloma, that's a good thought, and after reading your suggestion, I checked that twice to make sure. It seems that it is indeed trying to write the file to the relative location of the server's root directory.(same folder the php script itself is saved in)

However, after looking at it a bit, I found that what happened is that the PHP was writing this line of code to the final document:
[code=html]<input type="text" size="75" />[/code]and it should have been:
[code=html]<input type="text" size="75" name="filename" />[/code]So when it was trying to write the text to the new file, it was trying to create the file "" which, well... doesn't work. ? :o my bad. Thanks both of you for trying to help me, even though it was only a typo.
Copy linkTweet thisAlerts:
@chesemonkylomaSep 05.2006 — ok, good!
×

Success!

Help @AmazingAnt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...