POSTing a file to a php server
Something that folks want to do a lot is to upload files (usually image files) to an online resource. I have covered this a lot with Google Drive, but this guide looks at how to upload files to a php server. The assumption is that you have an online http server running php with storage space with the correct configuration. many people have found that although they have this resource provided with their "free host" the upload facilities are not provided. For situations like that you may have to consider using THIS solution provided by Taifun. My efforts below are based upon the use of my own server, which has a fully configured php, and allowed file uploads without additional resources. I will also assume that you have ftp access to your online resource, in order to upload the required php file and create an images folder.
The first job is to setup the php file to receive the uploads:
PHP SCRIPT FILE
<?php
// Simple PHP script to save image file.
$imgDir = "images/";
$fileName = $_REQUEST['fname'];
$data = file_get_contents('php://input');
$store = file_put_contents($imgDir.$fileName, $data);
if ($store !== false ) {
echo "File $fileName saved to server, $store bytes\n";
} else {
echo "File $fileName not saved\n";
}
?>
This simple script uses the php://input method and the file_get and file_put contents methods to store a file. The output of file_put_contents on success is the number of bytes stored, so I can use this to test if the file is in place, and send this information back as the response content. The output if the file upload fails is false. Let us save this to a file called upload.php.
FTP in to your server and upload this file to the root folder of your domain. While there, also create a directory called images, and ensure it has correct ownership and permissions for the php file to write the uploaded file. You should end up with something like this:
domainname.co.uk
|-upload.php
|-images
You can of course create the directory structure of you choice for this, if you do not want upload.php in your root folder.
In your ftp program, open the images folder. You can now test that everything works using curl from your command line ( i work on Ubuntu Linux so will be using the command line requirements for linux).
curl --data-binary '@/path/to/file/test.png' -d 'fname=test.png' https://domainname.co.uk/upload.php
The --data-binary flag is important when calling/sending the file, as it keeps the file as a binary blob, and stops the php from trying to read the file contents, the -d section indicates the filename for the file, and uses the fieldname "fname" which is expected and requested in the php file.
When run, this should return:
File test.png saved to server, xxx bytes (with xxx being the size in bytes of the file test.png)
If it doesn't then you will need to look at your configuration so far! If you refresh your ftp program you should see test.png has appeared in the images folder. Once you have all this working, you can proceed to setting up your blocks in AI2....
BLOCKS
In the blocks, the file test.png is stored as an asset. You can see in the Web1.Url block how the fieldname "fname" is set. If your files to upload are in another directory of your device, you will need to split the filename off the file path which is usually generated when getting the file. For exampl a file on the sdcard will usually return a full file path of:
/storage/emulated/0/test.png
To get the filename only from this use the following blocks:
The Web1.GotText will return the response content in the same way as the curl command.