Search code examples
phppostfile-uploadapp-inventor

Upload a file with App Inventor 2 to a server through PHP


I am trying to upload a file with App Inventor 2 to a server through PHP. I followed the Photo Booth Android app tutorial, however server-side, myPhoto.jpg contains the filename, not the picture's content (e.g. myPhoto.jpg contains something like "file:///storage/emulated/0/Pictures/app_inventor_1424997354199.jpg"). How can I fix it?

The code I use:

enter image description here

tempSaveFile.php :

<?php
 
$dataToWrite = $_REQUEST['fileName'];
$fileName = "myPhoto.jpg";     
file_put_contents($fileName, $dataToWrite);
 
?>

I am aware of Taifun's tutorial but since in my php.ini always_populate_raw_post_data = On I would prefer to avoid having to install anything.

Scott's tutorial seems to do something similar (with App Inventor 1):

enter image description here

enter image description here


Solution

  • in the URL you should only transfer the filename without path, e.g. app_inventor_1424997354199.jpg

    in the PostFile block you should use the complete path, e.g. file:///storage/emulated/0/Pictures/app_inventor_1424997354199.jpg

    enter image description here

    then on the server, try Scott's solution

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