A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: read and write to multiple text files with as3 and php

  1. #1
    Member
    Join Date
    Apr 2010
    Posts
    50

    read and write to multiple text files with as3 and php

    I am trying to work with code from http://www.kirupa.com/forum/showthread.php?t=306368

    that uses a php script to read from and write to a file called "text.txt" using the php file "write.php". I want to be able to have multiple of these boxes on one flash page, writing to different files. but I am not sure how to duplicate the code (or split it to apply to multiple files and entry boxes) and put it all on one page, etc. I need it to be able to pull from separate entry boxes and then write to the associated files for each box.

    here is the flash code:
    Code:
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    stop();
    
    var variables:URLVariables = new URLVariables();
    var varSend:URLRequest = new URLRequest("write.php");
    var varLoader:URLLoader = new URLLoader;
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;
    
    bt_sub.buttonMode = true;
    bt_sub.addEventListener(MouseEvent.CLICK, ValidateAndSend);
    
    
    function ValidateAndSend(event:MouseEvent):void
    
    {
       
        gotoAndPlay(2);
         variables.wootext = text_txt.text;
    
               varLoader.load(varSend);
       
    }
    
    
    
    
    
    
    var url:String = "test.txt";
    var loadit:URLLoader = new URLLoader();
    loadit.addEventListener(Event.COMPLETE, completeHandler);
    loadit.load(new URLRequest(url));
    
    function completeHandler(event:Event):void
    {
    text_txt.text = event.target.data as String;   
    }
    here is php:
    Code:
    <?php
    
    $wootext   = $_POST['wootext'];
    
    
    // Strip slashes on the Local variables
    $wootext   = stripslashes($wootext);
    
    
    
    
    
    
    $filename = 'test.txt';
    $somecontent = "Add this to the file\n";
    $somecontent2 = "Add this to the file2\n";
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    
        // In our example we're opening $filename in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $somecontent will go when we fwrite() it.
        if (!$handle = fopen($filename, 'wb')) {
             echo "Cannot open file ($filename)";
             exit;
        }
    
        // Write $somecontent to our opened file.
        if (fwrite($handle, $wootext) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
        }
    
        echo "Success, wrote ($wootext) to file ($filename)";
    
        fclose($handle);
    
    } else {
        echo "The file $filename is not writable, or doesn't exist";
    }
    ?>
    thanks for the help

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    this sounds like you will want to move to a database later....

    You probably should create copies of write.php, and have a fixed file name in every copy, rather than try to send the filename along with the data

    Musicman

  3. #3
    Member
    Join Date
    Apr 2010
    Posts
    50
    I really only need to to write to a couple of files. I am trying to put the text content of a website in each file and then have a web interface that allows editing of the content. I am just confused at how to code it without reproducing the entire flash code and changing the name of all variables. it seems like there should be a way to only change key parts.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center