Welcome to WebmasterWorld Guest from 54.204.106.194

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple Page Editor

     
2:30 pm on Jun 26, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Mar 22, 2008
posts: 317
votes: 0


I'm making a *VERY* simple page editor. It has a textarea which displays source code for an HTML page. When the form is submitted, it's supposed to use file_put_contents() to update the HTML page. The script is executing properly (i.e. valid syntax) but it never actually updates the page with the new content. It just shows the "error your changes were not saved" message. Permissions for this file are 777 (owner/group/public read/write/execute). What could be the problem?

<?php

$page=$_REQUEST['page'];
$file=$page.".html";
$header = '<!--#include file="header" -->';
$footer = '<!--#include file="footer" -->';


if(isset($_POST['content'])) {
// UPDATE HTML PAGE
$contents = $header."\n".$_POST['content']."\n".$footer;
if(file_put_contents($file,$contents)!=false) {
echo $file;
echo $contents;
}
else { echo "Error! Your changes were not saved :("; }
}
else {
// SHOW EDITOR
$content = file_get_contents($file);
$content = str_replace($header,"",$content);
$content = str_replace($footer,"",$content);
echo "<form action='editor.php' method='post'>\n
<h1>Editing: $page</h1>\n
<input type='hidden' name='page' value='$page' />
<textarea name='content' style='width: 100%; height: 85%;'>$content</textarea>
<input type='submit' value='Save' />
</form>";

}

?>
3:19 pm on June 26, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Mar 22, 2008
posts: 317
votes: 0


UPDATE: I also tried using the fwrite() function and the result is the exact same -- the script executes, but does not update the file.

UPDATE #2: I added error reporting. Turns out permission was denied for both fopen/fwrite and file_put_contents.

So now the question becomes: are there alternatives to fwrite and file_put_contents? Or is there a way to enable these functions using .htaccess?
3:50 pm on June 26, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15756
votes: 0


first, the return from file_put_contents is an integer or false so to properly test the return you need to type check as well

if(file_put_contents($file,$contents) !== false) {

it's odd to use both $_REQUEST and $_POST to access different parts of the same posted vars

just permission denied to the file or to the function itself?
3:51 pm on June 26, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10684
votes: 33


you probably need to change the permissions or ownership for the directory in which you are trying to create this file.
5:11 pm on June 26, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Mar 22, 2008
posts: 317
votes: 0


phranque is correct! Modifying the permissions for the directory did the trick. Thanks guys.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members