Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: PHP File Writing

  1. #1
    Join Date
    Apr 2008
    Posts
    38
    Thanks
    9
    Thanked 1 Time in 1 Post

    Angry PHP File Writing

    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 Code:
    <?php
    function fileappend($txt$filenameFILE_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 />";

    ?>

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    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) {
    ?
    Edit:
    and $FILE_APPEND is not even used in your function.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. The Following User Says Thank You to fileserverdirect For This Useful Post:

    matthewbluewars (05-13-2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    38
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default

    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 Code:
    <?php
    function fileappend($txt$filename) {
    //Write to the blog file
    $file fopen($filename);
    $append file_put_contents($file$txtFILE_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 />";

    ?>

  5. #4
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    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 Code:
    <?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 />"

    ?>
    Last edited by fileserverdirect; 05-14-2008 at 05:41 PM. Reason: code error
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  6. The Following User Says Thank You to fileserverdirect For This Useful Post:

    matthewbluewars (05-17-2008)

  7. #5
    Join Date
    Apr 2008
    Posts
    38
    Thanks
    9
    Thanked 1 Time in 1 Post

    Exclamation

    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 Code:
    <?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 />";

    ?>

  8. #6
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Try This:
    $append = fwrite($file, $txt);
    where you previously had:
    $append = fwrite($file, $contents);
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  9. The Following User Says Thank You to fileserverdirect For This Useful Post:

    matthewbluewars (05-21-2008)

  10. #7
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Did you fix your code?
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  11. #8
    Join Date
    Apr 2008
    Posts
    38
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default

    Thanks! It works now and is fully functional.

  12. #9
    Join Date
    Apr 2008
    Posts
    38
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default

    Now have created a settings configuration page. For some reason it just shows up blank in the browser:
    Code:
    <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>
    Last edited by matthewbluewars; 05-24-2008 at 06:13 PM. Reason: For ease of reading it

  13. #10
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I have not tested this on my own enviorment, but I can see one error (add a semi-colon):
    Code:
    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.
    Last edited by fileserverdirect; 05-25-2008 at 09:47 PM. Reason: typo
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •