You probably also might be interested in my App Inventor FTP Extension, which does not require anything on the server side, it "just works"...
Scott already put together a nice example to post any file to 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 pick an image from your device and upload it to my server.
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 "POST any local file to a php server example" from Scott * * Date: 2013/Mar/05 * 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'); $filename = $_GET['filename']; if (file_put_contents($filename,$data)) { if (filesize($filename) != 0) { echo "File transfer completed."; } else { header("HTTP/1.0 400 Bad Request"); echo "File is empty."; } } else { header("HTTP/1.0 400 Bad Request"); echo "File transfer failed."; } } else { header("HTTP/1.0 400 Bad Request"); echo "Access denied"; //reports if accesskey is wrong } ?>
Q1:I follow your tutorial but I have an error "500 internal server error" while "post file".
A: Are you using GoDaddy? They are very restrictive about their settings.
Let me recommend 000webhost.com as web host, see banner below. It is very easy to work with them: just ftp the script and the PEAR package to a directory and it works
without a hassle, and the best is: their service is free! You have to use php://input instead of php://stdin with them, see also Q2 below.
Q2:I changed web hosting and now app response "File transfer failed." without error.
A:As you can see from the php script, this is one of our own error messages. Please check the log file on your server for
php error messages. You can try to change the following line
$data = php_compat_file_get_contents('php://stdin');
by the following
$data = php_compat_file_get_contents('php://input');
Q3:I am getting error 1104 everytime I click on the Post button (seems an error from android, before enter in PHP).
If I click by the second time, I get the file sent via php. Does anybody know how to get rid of error 1104?
A: This seems to be an issue with Samsung devices, see also this thread in the forum. You also can find a workaround there. Thank you Kelson!
Q4: I found this project in which you can upload an image to your server. Is this free or do I have to change the code to my own server?
A: You can use my server for your tests, but should use your own server for permanently use.
I will delete the images from time to time from my server.
Q5:I want to know how to send an image from the app to a database in my web host. I don't understand if the image is finally going to the ftp or something else,
because the page shows me "Access denied"
A: Yes, you can use this solution to send an image to your server. In the database just add the link to your uploaded image.
If you follow the steps and install the php script on your web server,
the image will be stored on your web server in the same directory you install the php script. If you call the
php script which is used to upload the image to the server using a browser,
you get the "Access denied" message. This is, because it actually does not make sense to open that page with a browser. Hope that helps!
Q6: I build this code, but when I want to test it, I get the error message "411 Length required. A request of the requested method POST requires a valid Content-length."
A: You are right, it does not work anymore on Strato. I now moved the example to 000webhost, see also Q1 above in case you like to use that free web host, too.
Btw. meanwhile I prepared an App Inventor extension, which can provide the length of a file, for more information see here.
Q7: I have downloaded "Post a file to your server in the internet". It works fine with your server. With my server if I choose a png or jpg file I get error 503.
My server is at One.com.
A: The error normally means "Service temporarily unavailable". See your web host support help about which server settings need to be changed.
Also this page might help to adjust your settings or others, just do a Google search.
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!
or donate some mBTC to Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Thank you! Taifun
Download aia file for App Inventor
Download php script Version 2
Back to top of page ...
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.