Problems using fopen (Accessing local file)

I am a webmaster for my organizations website and I’ve been trying to create a (password protected) webpage with a form that would allow me to append a local (and hopefully eventually remote) xml file which contains the news feed that the homepage display. I have the form working, and the login part works perfectly - I’m having issues with appending the file in question. Here is what I have for the code (it is activated upon pressing the “SUBMIT” button and using the method “POST”):

[php]

    <?php
    
    $dir =    "C://[FOLDER]/[FOLDER]/[FOLDER]";
    $file = "feed.xml";
    $local = $dir . "/". $file;
    
    
    // Load original file and delete last line
    $lines = file($local) or die("Error loading file - make sure local directory is in correct format and using correct slashes."); 
    $last = sizeof($lines) - 1 ; 
    unset($lines[$last]); 
    
    $fp = fopen($local, 'w'); 
    fwrite($fp, implode('', $lines)); 
    fclose($fp);
    
    //Set up strings
    $open = "    <content>";
    $title = "\t<title>" . $_POST['title'] . "</title>";
    $news = explode("|" , $_POST['news']);  //Seperates paragraphs
    $pix = explode("|" , $_POST['pix']);  //Seperates links
    $date = "\t<date>" . date('l F j, Y') . "</date>";
    $by = "\t<by>" . $_POST['by'] . "</by>";
    $close = "    </content>";
    $end = "</feed>";
    
    //Append to file (PHP_EOL creates a new line cuz \n wouldnt work for some reason)
    $f = fopen($local, 'a') or die("Could not open file");
    fwrite($f, PHP_EOL . $open . PHP_EOL);
    fwrite($f, $title . PHP_EOL);
    
    for ($x = 0; $x < count($news); $x++){
        fwrite($f, "\t<news>" . $news[$x] . "</news>" . PHP_EOL);
    }
    
    for ($x = 0; $x < count($pix); $x++){
        fwrite($f, "\t<pix>" . $pix[$x] . "</pix>" . PHP_EOL);
    }
    
    fwrite($f, $by . PHP_EOL);
    fwrite($f, $date . PHP_EOL);
    fwrite($f, $close . PHP_EOL);
    fwrite($f, $end);
    fclose($f);
   
    ?>
    
    Feed Updated - Please check site to make sure correct changes were made (Click <a href="[LINK]">here</a> to return)<br/>
    or <a href="[LINK]">Logout</a>
</body>

[/php]

The code doesn’t get very far - it dies when it first tries fopen(). It works when I test it on my server on the local host, but when I try to upload it to the server and run the code from there it won’t open the file. I’m thinking either (A) fopen wasn’t meant to be used the way I want it or (B) more likely I’m not directing it to the file correctly. I’m open to any suggestions, alteration or alternative methods to my end. Please and thank you.

Oh, and if it helps, my xml file is basically in this form:

<feed>
    <content>
        <title></title>
        <news></news>
        <pix></pix>
        <by></by>
        <date></date>
    </content>
    <content>
        ...
    </content>
    ....
</feed>

I don’t know if this was implied but I’ll mention it just in case:

For now I just want to be able to append a file at a particular directory in my computer (or anyones computer for that matter) and do nothing else with it. The code is written for one particular directory right now but it can be easily changed to pull the directory from the form I talked about.

Sponsor our Newsletter | Privacy Policy | Terms of Service