Results 1 to 5 of 5

Thread: code writes to single file, want to write to selected file

  1. #1
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default code writes to single file, want to write to selected file

    The following writes to a single text file, but I would like to change it to write to the selected file (file1, file2...so on) so more than one area can be updated. (only one area edited at a time though)
    PHP Code:
    <?php
    $data_file
    ='content.txt';
    $changes='';

    if(isset(
    $contents)) {
          
    $archive=fopen($data_file,'r+');
          
    flock($archive,2);
          
    ftruncate($archive,0);
          
    fputs($archive,stripslashes($contents));
          
    flock($archive,3);
          
    fclose($archive);
          unset(
    $contents);
         
    $changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/">View Changes [+]</a><br />';
    }

    function 
    getfile($filename) {
       
    $fd fopen($filename"rb");
       
    $content fread($fdfilesize($filename));
      
    fclose($fd);
       return 
    $content;
    }

    $data=getfile($data_file);
    ?>
    The idea scenario would be to have a dropdown box with the files listed so you can select the one you want to edit.

    PHP Code:
    $data_file = array('file1' => 'file1.txt''file2' => 'file2.txt''file3' => 'file3.txt''file34' => 'file4.txt'); 
    I changed $data_file to an array but I'm not sure how it would all tie together to write to the selected file.

    Select dropdown can possibly look like this.
    Code:
    <SELECT name="data_file">
    <OPTION value="file1">edit file1</OPTION>
    <OPTION value="file2">edit file2</OPTION>
    <OPTION value="file3">edit file3</OPTION>
    <OPTION value="file4">edit file4</OPTION>
    </SELECT>
    Any ideas/help would be appreciated. Thank you.

  2. #2
    Join Date
    Mar 2009
    Posts
    65
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default

    PHP Code:
    $selected_files = array ("file1""file2");


    foreach(
    $selected_files as $data_file)
    {
       
    $content get_file($data_file);
        
    $archive=fopen($data_file,'r+');
          
    flock($archive,2);
          
    ftruncate($archive,0);
          
    fputs($archive,stripslashes($contents));
          
    flock($archive,3);
          
    fclose($archive);
          unset(
    $contents);
         
    $changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/">View Changes [+]</a><br />'

    Last edited by CrazyChop; 04-10-2009 at 11:16 AM.

  3. #3
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you CrazyChop

    This seems like a nice short piece of code to make this work.
    foreach($selected_files as $data_file)

    When you login to edit files it uses the following URL
    index.php?status=connected

    What might be the best way to determine which file to edit?

    index.php?status=connected&selected_files=file1.txt
    with something like...
    PHP Code:
    if (isset($_GET['selected_files'])) {
    $selected_files $_GET['selected_files'];

    to get the page from the URL? $selected_files already sets the array in your code, so the above may not work. Thank you

  4. #4
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Can you try the following? Create files content.txt, file2.txt, file3.txt, file4.txt in a directory and save the following as a php page in the same directory...

    (I am not sure that this is what you exactly want to do.. But I haen't tested yet)


    Code:
    <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = GET>
    
    <table>
        <tr><td><b>Select a file to edit:</b></td></tr>
        <tr><td>
        <SELECT name="data_file">
          <OPTION value="content.txt">edit file1</OPTION>
          <OPTION value="file2.txt">edit file2</OPTION>
          <OPTION value="file3.txt">edit file3</OPTION>
          <OPTION value="file4.txt">edit file4</OPTION>
         </SELECT>
         </td></tr>
        
        <tr><td>
        <textarea rows = 3 columns = 20 name = "contents" ></textarea>
        </td></tr>
        
        <tr><td><b><input type = "submit" name = "submit" value = "Edit" ></b></td></tr>
        
    </table>
    
    
    </form>
    
    
    
     <?php
      
    function getfile($filename) {
       $fd = fopen($filename, "rb");
       $content = fread($fd, filesize($filename));
      fclose($fd);
       return $content;
    }
    
    
    
     if(isset($_GET['submit']))
     {
     $data_file = $_GET['data_file'];
     $contents = $_GET['contents'];
     
     
     $archive = fopen($data_file,'r+');
          flock($archive,2);
          ftruncate($archive,0);
          fputs($archive,stripslashes($contents));
          flock($archive,3);
          fclose($archive);
          unset($contents);
         $changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="' .  $data_file . '">View Changes [+]</a><br />';
         echo $changes;
    
     
     $data=getfile($data_file); 
     
     
     }
     
     ?>

  5. #5
    Join Date
    Jul 2008
    Posts
    136
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you borris83.

    This is working somewhat...seems to update the files, but working with it to get the initial content from the selected file that was already there.

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
  •