Save text to a file on SD card


How to save text to a file on SD card

Note: with the new File component, which has been introduced in release nb133 on May, 18th 2014, this workaround is now obsolete. Thank you MIT!

Scott already put together a nice example to save the contents of a TextBox, Label, variable, etc. to the SDCARD using a php server. Precondition to get this working is, to set the flag always_populate_raw_post_data to on on your server. Unfortunately some web hoster do not offer to change that flag. You can find it out with the following script how this is set on your server:

 <?php phpinfo(); ?> 

I also had this problem with my server, but then I found this solution on stackoverflow.com provided by powtac, which uses the PEAR::PHP_Compat package. I just copied the unzipped package to my server and modified the php script, see below to get this working. Woo Hoo! Thank you powtac and Scott!

If you can set the flag always_populate_raw_post_data to on, just use Scott's example, in this case there is no need for this workaround.

With this example you can save text from a textbox to a textfile on the SD card of your device. An Internet connection is required.

Important: Please use your own server for the php script! I will change the access key from time to time without further notice! Thank you.

You probably also might be interested in the SaveToSD Java solution.

App Inventor Blocks

To get the example working, use xF20y14w as access key.
Remember: you can use my server for test purposes, but later you should use your own server. Thank you.

PHP Script

I updated Scott's php script and added some basic security functionality from the script of Shival Wolf. Feel free to adjust it to your needs. Precondition to get this working on your server: The PEAR::PHP_Compat package is available there.

<?php
/*
 * Written By: Taifun
 * using parts from the "Web2SQL example" from ShivalWolf
 * and parts from the "Save the contents of a TextBox, Label, variable, etc. to the SDCARD example" from Scott
 *
 * Date: 2013/Feb/28
 * Contact: info@puravidaapps.com
 *
 * Version 2: 'dirname(__FILE__)' added to avoid problems finding the complete path to the script
 */

/************************************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
header('Cache-Control: no-cache, must-revalidate');

if($_GET['p']==$ACCESSKEY){
  // this is the workaround for file_get_contents(...)
  require_once (dirname(__FILE__).'/PHP_Compat-1.6.0a3/Compat/Function/file_get_contents.php');
  $data = php_compat_file_get_contents('php://input');

  if (strlen($data)>0) {
    echo $data;
  } else {
    header("HTTP/1.0 400 Bad Request");
    echo "File is empty.";
  }
} else {
  header("HTTP/1.0 400 Bad Request");
  echo "Access denied";     //reports if accesskey is wrong
}

?>

Do you need help with your App Inventor project?

I can help you: for one hour I charge 25 USD.
Please contact me at info [at] puravidaapps [dot] com for details!
For questions about App Inventor, please use the App Inventor forum. Thank you.

Screenshots

Download


If you find this example helpful and you are using it in one of your projects,
be nice and 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 | News | Forums | Links | Search | Contact