PDA

View Full Version : PHP File Writing



matthewbluewars
05-11-2008, 07:19 PM
I need help with a script. It was supposed to write the string 'blog.push(new Post(' . $_REQUEST['body'] . ', new Date(' . $_REQUEST['date'] . ')));/*\n*/' to the file blog.js, but it doesn't work. It just shows a blank page. If anyone could give me a replacement script, I'd be grateful.
But if anyone could debug the script below (of course I removed the username and password), I would also be grateful:

<?php
function fileappend($txt, $filename, FILE_APPEND) {
//Write to the blog file
$append = file_put_contents($filename, $txt);
if ($append)
echo 'File written...<br />';
else
die('Error writing file!<br />');
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$loggedin = (strtolower($username) == /*username not shown*/) && ($password == /*Password not shown*/);
if ($loggedin) {
fileappend('blog.push(new Post(' . $_REQUEST['body'] . ', new Date(' . $_REQUEST['date'] . ')));/*\n*/', 'blog.js');
} else
echo "Sorry, login failed.<br />";

?>

fileserverdirect
05-12-2008, 09:42 PM
Hi,
Just taking a quick look at it what is this: (bold)

function fileappend($txt, $filename, FILE_APPEND) {

I ran it through my php debugger and got this:
Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE in F:\a\dir\here\file.php on line 2

Did you mean somthing like:
function fileappend($txt, $filename, $FILE_APPEND) {
?

and $FILE_APPEND is not even used in your function.

matthewbluewars
05-13-2008, 08:56 PM
One of my problems was putting FILE_APPEND in the wrong place. Thanks for catching it. I still am having issues, though. Now my script reads:

<?php
function fileappend($txt, $filename) {
//Write to the blog file
$file = fopen($filename);
$append = file_put_contents($file, $txt, FILE_APPEND);
if ($append)
echo 'File written...<br />';
else
die('Error writing file!<br />');
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$loggedin = (strtolower($username) == 'username') && ($password == 'password');
if ($loggedin) {
fileappend('blog.push(new Post(' . $_REQUEST['body'] . ', new Date(' . $_REQUEST['date'] . ')));/*\n*/', 'blog.js');
} else
echo "Sorry, login failed.<br />";

?>

More info on file_put_contents. (http://www.w3schools.com/php/func_filesystem_file_put_contents.asp)
More info on PHP Filesystem functions. (http://www.w3schools.com/php/php_ref_filesystem.asp)

fileserverdirect
05-14-2008, 01:29 AM
I still am having issues, though.
What are the issues?
Is it not writing to the file, if so then I would recomend somthing like:


<?php
function fileappend($txt, $filename) {
//Write to the blog file
$file = fopen($filename, "a");
$append = fwrite($file, $txt);
fclose($file);
if ($append)
echo 'File written...<br />';
else
die('Error writing file!<br />');
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$loggedin = (strtolower($username) == 'username') && ($password == 'password');
if ($loggedin) {
fileappend('blog.push(new Post(' . $_REQUEST['body'] . ', new Date(' . $_REQUEST['date'] . ')));/*\n*/', 'blog.js');
} else
echo "Sorry, login failed.<br />";

?>

matthewbluewars
05-17-2008, 08:42 PM
I am now getting the "Error writing file" message. In the code I have inserted a comment to show where the message I'm getting comes from.

<?php
function fileappend($txt, $filename) {
//Open the file
$file = fopen($filename, "a");
/*
//I have commented this below section because it wasn't working
//get its contents
$raw = file_get_contents($filename);
//$raw .= $txt;
$contents = $raw . '//\n' . $txt;
*/
//now write it
$append = fwrite($file, $contents);
if ($append)
echo 'File written!<br />';
else {
//Here's the error message I'm getting:
die('Error writing file!<br />');
}
fclose($file);
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
//Check if the username and password are correct
$loggedin = (strtolower($username) == 'username') && ($password == 'password');
if ($loggedin) {
//Get the post to write
$body = $_REQUEST['body'];
//If the login passes, write the new post
fileappend('blog.push(new Post("' . $body . '", new Date("' . $_REQUEST['date'] . '")));', 'blog.js');
} else
//If the login fails, not good
echo "Sorry, login failed.<br />";

?>

fileserverdirect
05-18-2008, 01:40 AM
Try This:
$append = fwrite($file, $txt);
where you previously had:
$append = fwrite($file, $contents);
:)

fileserverdirect
05-20-2008, 09:27 PM
Did you fix your code?

matthewbluewars
05-21-2008, 02:05 AM
Thanks! It works now and is fully functional.

matthewbluewars
05-24-2008, 06:11 PM
Now have created a settings configuration page. For some reason it just shows up blank in the browser:

<html>

<head>

<title>Configuration Settings</title>

</head>

<body>
<form action="" method="get">
<textarea name="config" id="config"><?php
include('config.js');
?>
</textarea>
</form>
<?php
if(isset($_REQUEST['config'])){
//Open file
$config = fopen('config.js', 'w+');
if ($config)
echo 'File Opened! <br />'
//write file
fwrite($config, $_REQUEST['config']);
//close file
fclose($config);
}
?><br />
<script language="javascript" type="text/javascript">
function addToFavorites()
{
window.external.AddFavorite(document.location,'');
}

var bookLink = document.createElement("a");
bookLink.onclick = addToFavorites;
bookLink.appendChild(document.createTextNode("Bookmark this page!"));
bookLink.href = location.href;
if (window.location.search) {
bookLink.onclick = function() {var faveConfirm;
faveConfirm = window.confirm("We're sorry, but bookmarking the page now will cause issues.\nSo if you return to th\his exact url, " + window.location.href + "the configuration will return to the current settings. Are you sure you want to bookmark (handy for backing up the settings)?")
if (faveConfirm)
addToFavorites();
}
}
document.body.appendChild(bookLink);
</script>
</body>

</html>

fileserverdirect
05-24-2008, 11:00 PM
I have not tested this on my own enviorment, but I can see one error (add a semi-colon):


if ($config)
echo 'File Opened! <br />';


And is this supposed to be a differnt script or the same, beacause why are you including config.js if you are going to write to it, and not blog.js??? If your are going to use a flat file to store information for a single user, I would not recomend it at all. use SESSION varibles for a secure user-by-user basis. If you need help setting up your full blog system with a config page, just ask. :)

fileserverdirect
05-29-2008, 05:44 PM
Did you test it yet?
:)

matthewbluewars
05-29-2008, 09:14 PM
I have not tested this on my own enviorment, but I can see one error (add a semi-colon):


if ($config)
echo 'File Opened! <br />';


And is this supposed to be a differnt script or the same, beacause why are you including config.js if you are going to write to it, and not blog.js??? If your are going to use a flat file to store information for a single user, I would not recomend it at all. use SESSION varibles for a secure user-by-user basis. If you need help setting up your full blog system with a config page, just ask. :)

This is a different script, related to the original. It is supposed to be a blog hosting software, like WordPress.

fileserverdirect
05-30-2008, 07:05 PM
So does it work now? :confused:

matthewbluewars
05-31-2008, 04:17 PM
Everything BUT the configuration page works.

fileserverdirect
05-31-2008, 07:03 PM
Well, after correcting the semi-colon, there are no php errors that are showing up to me, even through the debugger. Could there be an error in your config.js?? Does your page show up blank still, or does it show up, but it is not writing to the config file?

fileserverdirect
06-05-2008, 09:00 PM
I am not sure what the problem is with your script, do you have a link online to the page? :)

P.S. I your User CP, make sure you check the box that emails you when you have a new post, because my last post was 7 days ago and you still haven't replyed...