Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Stupid PHP write to file!

    I want to pull in data which is contained in a separate file, this data will be added to the textarea which will allow editing.

    Once finished editing, I want it to save to the same file and over write the existing stuff...

    Error:
    PHP Code:
    Warningfopen(includes/Content.php) [function.fopen]: failed to open streamNo such file or directory in /home/martynba/public_html/stokeGTA/editor.php  on line 25

    Warning
    fread(): supplied argument is not a valid stream resource in /home/martynba/public_html/stokeGTA/editor.php on line 26

    Warning
    fclose(): supplied argument is not a valid stream resource in /home/martynba/public_html/stokeGTA/editor.php on line 27 
    Actual page:
    PHP Code:
    <?php 
    session_start
    (); 
    if(isset(
    $_SESSION['session'])){ 
    //user is logged-in, so do nothing 

    else{ 
    //user needs to log in. 
    header ("location: scripts/php/login.php"); 
    }
    //Get other values for identification
        //Post topic
    $pageTopic $_GET['page'];
        
    //Username
    $username =  $_SESSION['session'];
    $username explode("."$username);
    $username $username[0];
        
    //Date
    $date date('l jS \of F Y h:i:s A');
    if (
    $pageTopic != "") {
        }
        else {
        echo 
    "URl variable not set, please load page using given link, <a href=\"index.php\">Back!</a>";
        }
    $myFile "includes/$pageTopic"."Content.php";
    $fh fopen($myFile'r');
    $theData fread($fh10000000);
    fclose($fh);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Page Editor</title>
    <link rel="stylesheet" href="../stokeGTA/css/mainsheet.css"/>
    <script type="text/javascript" src="scripts/js/hoverFix.js"></script>
    </head>
    <body>
    <div class="container" style="padding:3em 0em;">
    <span class="editorButton">AL</span>
    <span class="editorButton">AM</span>
    <span class="editorButton">AR</span>
    <span class="editorButton">B</span>
    <span class="editorButton">I</span>
    <span class="editorButton">U</span>
    <span class="editorButton">JS</span>
    <span class="editorButton">JS</span>
    <form name="editPage">
    <textarea class="field" cols="120" rows="20"><?php echo "$theData"?></textarea><br />
    <input type="submit" name="saveFile" value="Save File">
    </form>
    <?php 
    echo "";
    if (isset(
    $_POST['saveFile'])) {
    $fname "includes/".$pageTopic."Content.php";
    $fhandle fopen($fname,"r");
    $content fread($fhandle,filesize($fname));

    $content str_replace("oldword""newword""$theData");

    $fhandle fopen($fname,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);
    if (!
    $fwrite) {
    echo 
    "Error saving file!";
    } else {
    echo 
    "Successfully saved file!";
    }
    }
    ?>
    </div>
    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This is what it can't find:

    $myFile = "includes/$pageTopic"."Content.php";

    So maybe you meant to do this?

    $myFile = "includes/".$pageTopic."."Content.php";

    Or is $pageTopic a directory name?

    $myFile = "includes/".$pageTopic."/Content.php";

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    $pageTopic is the URL variable which is used to make something like this:
    weaponContent.php / vehicleContent.php

    It wouldn't display the content in the textarea if $myFile wasn't working though :S
    Last edited by martynball; 01-25-2010 at 11:07 PM.

  • #4
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    try this. post results

    PHP Code:
    <?php 
    session_start
    (); 
    if(isset(
    $_SESSION['session'])){ 
    //user is logged-in, so do nothing 

    else{ 
    //user needs to log in. 
    header ("location: scripts/php/login.php"); 
    }
    //Get other values for identification
        //Post topic
    $pageTopic $_GET['page'];

        
    //Username
    $username =  $_SESSION['session'];
    $username explode("."$username);
    $username $username[0];
        
    //Date
    $date date('l jS \of F Y h:i:s A');

    if(!isset(
    $pageTitle) || $pageTitle=="") {
        echo 
    "URl variable not set, please load page using given link, <a href=\"index.php\">Back!</a>";
    } else {
    $myFile "includes/$pageTopic"."Content.php";
    $fh fopen($myFile'r');
    $theData fread($fh10000000);
    fclose($fh);
    }
    //rest of your code
    ?>
    also ensure that your $_GET is actually "page" so $pageTitle is actually getting set. the error messge you posted suggests $pageTitle is NOT set/has no value.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    The 2 things in red (below) ... that's suspicious ... Topic or Title?


    $pageTopic = $_GET['page'];

    //Username
    $username = $_SESSION['session'];
    $username = explode(".", $username);
    $username = $username[0];
    //Date
    $date = date('l jS \of F Y h:i:s A');

    if(!isset($pageTitle) || $pageTitle=="") {


  •  

    Posting Permissions

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