APIs: POST any local file to a php server

12,701 views
Skip to first unread message

Scott Ferguson

unread,
Sep 30, 2012, 5:59:29 PM9/30/12
to app-inventor-de...@googlegroups.com

This test configuration works for me, but I cannot guarantee it will work on other servers than with the hosting service I use (000webhost.com). Oddly enough it requires no header information to be entered from App Inventor, yet allows use of the Web component's PostFile block with the added bonus that I did not have to figure out how to tell the server what type of data I was sending in a Content-Type entry. It is just a data stream that the server captures and converts back into a file. The php $_GET variable works also to pass the file name for the destination file as a switch on the URL.

Well, here it is...
web_post_file_test.zip
postfiletest.php
Message has been deleted

Zach Soflin

unread,
Jan 28, 2013, 4:51:50 PM1/28/13
to app-inventor-de...@googlegroups.com
any ideas why I would get a 403 forbidden and 404 Not found when using this? have been trying for hours and can't get the file to upload. I suspect it has something to do with the data stream, but am not experienced in php. Any ideas?

Scott Ferguson

unread,
Jan 30, 2013, 3:05:54 AM1/30/13
to app-inventor-de...@googlegroups.com
Make sure that your permissions to the directory folder on the server and to the file being written to allow write access and that the php script has execute access. Other than that, I don't know. I posted a question once on the PHP forum but got no response whatsoever so don't expect much help there.
---sfs
Message has been deleted

Zach Soflin

unread,
Feb 6, 2013, 10:18:03 AM2/6/13
to app-inventor-de...@googlegroups.com
Thanks for the quick reply Scott, I must have missed the alert email because I just saw your response, sorry. you were right, I use Hostgator and they had to whitelist the script for it to work. I now can send the POST request from App Inventor, and get the file to show up in the directory. The problem is the file is empty. Seems the data stream isn't working. Any ideas? did you have this problem? Thanks again for the help, I have been scouring the web for an answer to this. 
-Zach

Taifun

unread,
Feb 6, 2013, 10:50:46 AM2/6/13
to app-inventor-de...@googlegroups.com
see here http://php.net/manual/en/wrappers.php.php
as it seems to be, always_populate_raw_post_data is set to false on your server. You can find that out, with the following script:

<?php
phpinfo();
?>

but some web hoster do not allow to change that parameter...
see also here http://stackoverflow.com/questions/9907896/what-are-the-alternatives-for-php-input-and-http-raw-post-data-when-file-get

let us know if you get it running...
probably I should try that http://stackoverflow.com/a/10320453/1545993 myself too if I find some time?
Taifun


php://input

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".


Zach Soflin

unread,
Feb 6, 2013, 12:06:19 PM2/6/13
to app-inventor-de...@googlegroups.com
Thanks for the quick reply Taifun, 
Hum... I contacted HostGator and got always_populate_raw_post_data enabled. Using phpinfo() i can see that it is enabled. I am still getting a blank file in my directory with no errors anywhere. I guess the only problem showing is when I hit the "Post" button on Scotts example, It gives me the error "1104 unable to Post file..." but if I push the button again the response reads "file transfer completed".and the blank file shows up on my web server. Any other ideas? This is stumping me.
-Zach

Zach Soflin

unread,
Feb 6, 2013, 12:42:55 PM2/6/13
to app-inventor-de...@googlegroups.com
GOT IT! wow that took way too long, but I found a post from Scott that said to try swapping out php://input with php://stdin. It worked! So anyone using host gator make sure to enable always_populate_raw_post_data by contacting customer service and begging and pleading (they are pretty good about it). Then using Scotts example but swap out php://input for php://stdin. Thanks for the direction you two.
-Zach

Scott Ferguson

unread,
Feb 6, 2013, 9:29:11 PM2/6/13
to app-inventor-de...@googlegroups.com
Glad you got it working, Zach! And thanks Taifun :)
---sfs

Taifun

unread,
Feb 8, 2013, 12:55:38 PM2/8/13
to app-inventor-de...@googlegroups.com
I now played around with the workaround mentioned above and put together an example here http://puravidaapps.com/postfile.php
This workaround is useful, if you can't set the flag always_populate_raw_post_data = on on your server
Taifun

Scott Ferguson

unread,
Feb 9, 2013, 4:19:36 PM2/9/13
to app-inventor-de...@googlegroups.com
Very nice -- and that Pear package provides the workaround on the PHP end.
---sfs

Zach Soflin

unread,
Feb 11, 2013, 7:46:49 PM2/11/13
to app-inventor-de...@googlegroups.com
Hi again,
Do either of you know if you can detect the error 1104 when it pops up? I essentially have this script uploading the image every time the camera takes a picture. It works 75% of the time, but sometimes I get an error bubble that pops up for a few seconds then disappears  it says, "error 1104" and then says it wasn't able to POST the file. I would like to be able to detect it so I can have the app try again. Any ideas?
-Zach

Taifun

unread,
Feb 11, 2013, 8:22:25 PM2/11/13
to app-inventor-de...@googlegroups.com
you can try to catch the error with the Screen.ErrorOcurred block
Taifun


Am Montag, 11. Februar 2013 18:46:49 UTC-6 schrieb Zach Soflin:
Hi again,
Do either of you know if you can detect the error 1104 when it pops up? I essentially have this script uploading the image every time the camera takes a picture. It works 75% of the time, but sometimes I get an error bubble that pops up for a few seconds then disappears  it says, "error 1104" and then says it wasn't able to POST the file. I would like to be able to detect it so I can have the app try again. Any ideas?
-Zach

On Wednesday, February 6, 2013 11:42:55 AM UTC-6, Zach Soflin wrote:
GOT IT! wow that took way too long, but I found a post from Scott that said to try swapping out php://input with php://stdin. It worked! So anyone using host gator make sure to enable always_populate_raw_post_data by contacting customer service and begging and pleading (they are pretty good about it). Then using Scotts example but swap out php://input for php://stdin. Thanks for the direction you two.
-Zach

On Wednesday, February 6, 2013 11:06:19 AM UTC-6, Zach Soflin wrote:
Thanks for the quick reply Taifun, 
Hum... I contacted HostGator and got always_populate_raw_post_data enabled. Using phpinfo() i can see that it is enabled. I am still getting a blank file in my directory with no errors anywhere. I guess the only problem showing is when I hit the "Post" button on Scotts example, It gives me the error "1104 unable to Post file..." but if I push the button again the response reads "file transfer completed".and the blank file shows up on my web server. Any other ideas? This is stumping me.
-Zach

On Wednesday, February 6, 2013 9:50:46 AM UTC-6, Taifun wrotey to

Zach Soflin

unread,
Feb 12, 2013, 11:25:59 PM2/12/13
to app-inventor-de...@googlegroups.com
Perfect, thanks, exactly what I was looking for!
-Zach

Simon Carr

unread,
Mar 18, 2013, 3:26:07 PM3/18/13
to app-inventor-de...@googlegroups.com
Hi Scott,

Thank you for this. I have a problem however. My server just saves an empty (0kb) image file. Any ideas what I might be doing wrong?

Simon

Taifun

unread,
Mar 18, 2013, 3:37:44 PM3/18/13
to app-inventor-de...@googlegroups.com
are you able to set the flag always_populate_raw_post_data = on ?
Taifun

Simon Carr

unread,
Mar 18, 2013, 3:53:41 PM3/18/13
to app-inventor-de...@googlegroups.com
Yep, I already did that, then restarted apache and checked my php.ini which says it is on.

I have tried php://input, php://stdin and $HTTP_RAW_POST_DATA

I tried echo'ing out the value of $data but it is empty so I am not sure if my phone is even sending any data in the post. I am using the unaltered provided by your self. I just changed the URL and the get variable from filename to fn to suit scott's PHP script.

I am using a Samsung Note 2 and am using a VPS server hosted by 123-reg.

Here is the php script

<?PHP
   $data = file_get_contents('php://input');
echo $data;
   if (!(file_put_contents($_GET['fn'],$data) === FALSE)) echo "File xfer completed."; // file could be empty, though
   else echo "File xfer failed.";
?>


Simon

Taifun

unread,
Mar 18, 2013, 4:50:22 PM3/18/13
to app-inventor-de...@googlegroups.com

Scott Ferguson

unread,
Mar 18, 2013, 6:24:25 PM3/18/13
to app-inventor-de...@googlegroups.com
Have you tried Content-Type image/png or image/jpg etc. for your Headers?
---sfs

Péter Simon

unread,
Nov 3, 2013, 5:37:00 AM11/3/13
to app-inventor-de...@googlegroups.com
hey, can u help me, how can i set the flag always_populate_raw_post_data to on on my server?  if u can please explain this for me like u can explain this to a 5 yrs old child.. :D 

Scott Ferguson

unread,
Nov 3, 2013, 6:03:14 AM11/3/13
to app-inventor-de...@googlegroups.com
You could try this workaround at the top of a php script: http://www.rdlt.com/always_populate_raw_post_data-disabled-heres-a-workaround.html
This person set the flag to on in his script: http://pastebin.com/qvF6PeyR
Example entry in php.ini:
always_populate_raw_post_data = On
---sfs

Péter Simon

unread,
Nov 3, 2013, 8:09:21 AM11/3/13
to app-inventor-de...@googlegroups.com
thanks for the fast reply! now i found an ftp server with anonymus acces, but my app isnt working. can u tell me Scott what is the problem? :)
help.jpg

Taifun

unread,
Nov 3, 2013, 9:15:37 AM11/3/13
to app-inventor-de...@googlegroups.com
there is no ftp component in App Inventor, star issue 1111 https://code.google.com/p/app-inventor-for-android/issues/detail?id=1111
you will need a php script on the server to be able to upload a file
In case you can't set the flag always_populate_raw_post_data = on on your server, you can try my workaround http://puravidaapps.com/postfile.php
 Taifun

Kevin Darden

unread,
Nov 16, 2013, 2:38:19 PM11/16/13
to app-inventor-de...@googlegroups.com
What is the file being written to?  Do I need to create a file like Get Images.php under the public_html directory? Would this then be the file?  Do I need to modify something in the designer to match this? Do I modify something in the code block to match this?


On Wednesday, January 30, 2013 2:05:54 AM UTC-6, Scott Ferguson wrote:

Scott Ferguson

unread,
Nov 17, 2013, 7:12:48 AM11/17/13
to app-inventor-de...@googlegroups.com
I have requested that you attach the current zip project and insert the php script text in your other post. Thanks.
---Scott

Jon Reneberg

unread,
Dec 15, 2013, 5:54:24 PM12/15/13
to app-inventor-de...@googlegroups.com
The file name of the file on your Android device cannot have a space in it, or it will stop at the space.  Some quick code could replace spaces with underscores.

I am also getting the full file name saved with 0kb.  Is this a permission issue?  How would I find out if my Android device is sending the full file rather than just the name?

Taifun

unread,
Dec 15, 2013, 6:02:27 PM12/15/13
to app-inventor-de...@googlegroups.com
I am also getting the full file name saved with 0kb.  Is this a permission issue?  
in the php script try to replace php://input by php://stdin

 How would I find out if my Android device is sending the full file rather than just the name?
the postfile block sends a file and not a text
probably that workaround can help, too: http://puravidaapps.com/postfile.php 
Taifun

Jon Reneberg

unread,
Dec 15, 2013, 6:37:20 PM12/15/13
to app-inventor-de...@googlegroups.com


On Sunday, December 15, 2013 6:02:27 PM UTC-5, Taifun wrote:
I am also getting the full file name saved with 0kb.  Is this a permission issue?  
in the php script try to replace php://input by php://stdin

Hmmm...  Still get the file with 0kb.
When I press the "Post" button, it takes 90 seconds or so before I get the "File xfer completed." message which leads me to believe that the Android app is uploading the full image, but it's somehow not being saved correctly by the server.

Scott Ferguson

unread,
Dec 15, 2013, 7:23:28 PM12/15/13
to app-inventor-de...@googlegroups.com
Are you sure that you have write access to the destination folder?
---
Scott

Kelson Fox

unread,
Mar 1, 2014, 8:48:47 AM3/1/14
to app-inventor-de...@googlegroups.com
Hi Scott

Hope you can help me...
Every time I click to post file, I get "error 1104", then if I click second time the file is sent.

How to get rid of this?


Scott Ferguson

unread,
Mar 2, 2014, 8:32:53 PM3/2/14
to app-inventor-de...@googlegroups.com
I don't know any more than Taifun and Hossein.
---
Scott

kenma...@gmail.com

unread,
Apr 21, 2014, 7:48:26 AM4/21/14
to app-inventor-de...@googlegroups.com
Hi,

We run a coding session for kids here in Ireland.

We are doing an app inventor session to upload images (to a database so the kids can share them). As step one we want to post an image to the directory. However getting zero file sizes. Installed on a local WAMP server or hosted. So we put the whole problem into two files, one to post a file and one to write to remove any possible problems (we have installed the workaround package and fail). Seems that the data from the actual file is just not coming across. 

As its for teach kids, we hoped you would help us :-).

Regards,
M
Ps. outputs on the webbrowser is:
'fakename.jpg
File transfer failed.'

The directory has 'fakename.jpg' with zero bytes.
dojo-save.php
savepicture.php

Taifun

unread,
Apr 21, 2014, 10:35:24 AM4/21/14
to app-inventor-de...@googlegroups.com
as already suggested in this thread:
1. are you able to set the flag always_populate_raw_post_data = on ?
2. in the php script try to replace php://input by php://stdin
3. probably that workaround can help, too: http://puravidaapps.com/postfile.php 
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

Zoran Kukulj

unread,
Apr 21, 2014, 11:29:25 AM4/21/14
to app-inventor-de...@googlegroups.com
Hi,
Are YOU sure That's the folder ,where picture shouuld ne uploaded, in WAMP (on server) is writable?
--
(you have received this message from the App Inventor Developers Library)
---
You received this message because you are subscribed to the Google Groups "App Inventor Developers Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-develope...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kenma...@gmail.com

unread,
Apr 21, 2014, 12:46:53 PM4/21/14
to app-inventor-de...@googlegroups.com
Thank you for the quick reply.

Adding the following line works.

file_put_contents("test.txt","can I write");

This works.

In other words the $data variable seems to be empty.

I have tried the stdin and the input.

Hosting is on 000webhost.com

I cannot change the setting  for the on/off item mention above as we need to use a free hosting server (its a volunteer based training, no charge).

Do the files we attached work for you? Could you recommend an alternative hosting site?

Taifun

unread,
Apr 21, 2014, 1:47:04 PM4/21/14
to app-inventor-de...@googlegroups.com
000webhost.com works fine together with my workaround http://puravidaapps.com/postfile.php 
Taifun

Zoran Kukulj

unread,
Apr 21, 2014, 1:48:01 PM4/21/14
to app-inventor-de...@googlegroups.com
Hi,
000webhost.com worked for me as well, so most likely it is not anything related to hosting free.





--

no name

unread,
May 1, 2014, 7:43:51 AM5/1/14
to app-inventor-de...@googlegroups.com
Where do I put the .php file?

Taifun

unread,
May 1, 2014, 9:40:04 AM5/1/14
to app-inventor-de...@googlegroups.com
Where do I put the .php file?
in a directory onto your web server, which is accessible from the internet
Taifun

no name