App Inventor and the Web component


How to save text to a file in the Internet

Note: with the extension feature, this solution is now obsolete. Thank you MIT!
Now you can use my FTP Extension, which does not require anything on the server side, it "just works"...


Using the web component and a php script on the server we can save text in a file on the server. I used the Web2MySQL example from Shival Wolf as template and modified it accordingly. You can adjust the php script to your needs (e.g. using diffferent file names etc.).

php Script

<?php
/*
 * Written By: Taifun (by using the Web2SQL example from ShivalWolf)
 * Date: 2011/12/18
 * Contact: info@puravidaapps.com
 */

 /************************************CONFIG****************************************/

//SETTINGS//
//This code is something you set in the APP so random people cant use it.
$ACCESSKEY="secret";

/************************************CONFIG****************************************/

//these are just in case setting headers forcing it to always expire and the content type to CSV
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: text/csv');

error_log(print_r($_POST,TRUE));

if( isset($_POST['export']) && isset($_POST['key']) ){  // checks if the tag post is there and if its been a proper form post
  if($_POST['key']==$ACCESSKEY){                        // validate the ACCESS key
    $export=urldecode($_POST['export']);
    $outstream = fopen("./export.txt", 'w');
    fputs($outstream, $export);
    fclose($outstream);
    echo $export;                                       // writes out csv data back to the client
  } else {
    header("HTTP/1.0 400 Bad Request");
    echo "Bad Request";                                 // reports if the code is bad
  }
} else {
  header("HTTP/1.0 400 Bad Request");
  echo "Bad Request";
}
?>


Note: In case you need different file names, the simplest solution would be to replace the following line in the php script
  $outstream = fopen("./export.txt", 'w'); by this line
  $outstream = fopen('./export'.date("Y.m.d.H:i:s").'.txt', 'w');
Then you will have a filename with the current server date/time. More about the different date/time options see here.

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download aia file for App Inventor
Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact