Welcome to WebmasterWorld Guest from 23.20.241.155

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Simple Page Editor

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

5+ Year Member



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 Jun 26, 2013 (gmt 0)

5+ Year Member



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 Jun 26, 2013 (gmt 0)

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



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 Jun 26, 2013 (gmt 0)

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



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 Jun 26, 2013 (gmt 0)

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month