homepage Welcome to WebmasterWorld Guest from 54.227.62.141
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Simple Page Editor
webfoo




msg:4587848
 2:30 pm on Jun 26, 2013 (gmt 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>";

}

?>

 

webfoo




msg:4587861
 3:19 pm on Jun 26, 2013 (gmt 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?

jatar_k




msg:4587875
 3:50 pm on Jun 26, 2013 (gmt 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?

phranque




msg:4587876
 3:51 pm on Jun 26, 2013 (gmt 0)

you probably need to change the permissions or ownership for the directory in which you are trying to create this file.

webfoo




msg:4587910
 5:11 pm on Jun 26, 2013 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved