edit file with PHP

Discussion in 'Web Design & Coding' started by CHiLLaXen, Feb 12, 2004.

  1. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    ok, I've just started php coding and I need some help. I need it to open a file and write to it, but I want it to write at the beginning of the file not the end of it. How can I do that?
     
  2. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
  3. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    I'm sure there's a way to actually write from the beginning, I used to have a file that did it, however this would seem the easiest method..
    PHP:
    function write_beg($filename$data

    //Imports old data 
    $handle fopen($filename"r"); 
    $old_content fread($handlefilesize ($filename)); 
    fclose($handle); 

    //Sets up new data 
    $final_content $data.$old_content

    //Writes new data 
    $handle2 fopen($filename"w"); 
    $finalwrite fwrite($handle2$final_content); 
    fclose($handle2); 
    }
     
  4. Nick

    Nick OSNN Lurker

    Messages:
    147
    Location:
    North West, UK
    Opening it with mode 'w' should give you the start of the file and 'a' should give you the end of the file. Alternatively 'r+' gives you read/write access from the begining of the file, but won't create the file if it doesn't exist like 'w' will.
     
  5. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I used the following as a style.css editor, I have modified it down for you though. Basically the method it uses is to open the file you want to edit and then read the contents into a variable which is then placed in a <textarea>$variable</textarea> which makes it show up in the text area of the form. you can then modify that and submit at which point the data in the textarea on submission is then written to the original file and the contents of the original file is written to a backup file.

    PHP:
    <?php
    // setup stylesheet modification process
    if ($_POST['submit'] == "update file") {
        
    $filename "./style.css"/* file to write to could be changed to a variable passed by the form */
        
    $copyname "./style.copy.css"/* using this makes a backup copy again this could be changed to a variable and have .bak appended to the filename */
        
        // open the style.css file to make a copy of the file
        
    $css_r fopen($filename"r");
        
    $style_copy fread($css_rfilesize($filename));
        
    fclose($css_r);
        
        
    // as changes have been made lets make a backup
        
    $css_copy fopen($copyname"w");
        
    $fcopy_out fwrite($css_copy$style_copy);
        
    fclose($css_copy);
        
        
    // copy is made now lets update the style.css file
        
    $css_w fopen($filename"w");
        
    $fstyle_out fwrite($css_w$_POST['contents']);
        
    fclose($css_w);
        
        
    // check if there were any problems writing to files.
        
    if ($fstyle_out != strlen($_POST['contents']) || $fcopy_out != strlen($style_copy)) {
            die (
    "Error writing to file\nPlease make sure the directory\nis CHMOD 777");
        } else {
            
    header ("Location: file_written.html"); /* this just redirects you to a new page which says the file has been written to */
        
    }
    } else {
        
    // open the style.css file and get the contents for the form
        
    $filename "./style.css"/* You could change this to modify a different file or you can get the $filename variable from another form or something */
        
    $fp fopen($filename"r");
        
    $contents fread($fpfilesize($filename));
        
    fclose($fp);
        
        
    // setup html vars
    $xml "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">";
    $html = <<< EOF
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Yoric's Place</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    <!--
    input {
        border: 1px solid #000000;
    }
    textarea {
        border: 1px solid #000000;
    }
    -->
    </style>
    </head>
    <body>
      <h2>File Modifier</h2>
        <form action="
    {$_SERVER['PHP_SELF']}" method="post">
          <p><br />
          <textarea name="contents" cols="40" rows="20" id="contents" wrap="none">
    {$contents}</textarea>
          <br />
          <input name="submit" type="submit" id="submit" value="update file" />
          </p>
        </form>
    </body>
    </html>
    EOF;

    echo 
    $xml;
    echo 
    $html;
    }
    ?>
     
  6. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    wow, should have looked at this post sooner, thanks for the help