public_html for local file exchange
I was thinking of finding a way to easily exchange files on my android mobile phone to my computer. My computer is already set up with a full LAMP stack and I had setup a public_html folder for testing of local files. This simply needed extending to offer a php file upload script, and a php file listing script (for selecting a file to download). However, things are never that simple....
Create the public_html configuration an allow php to run
Setup public_html
This provides a user with the ability to access files placed in a ~USER directory through a web browser. In the following, replace USER with your user name.
Open up a terminal and do the following:
~$ mkdir -p ~/public_html
~$ sudo a2enmod userdir
~$ chmod -R 755 ~/public_html
~$ sudo systemctl restart apache2
If you now place an html file (e.g. index.html) in the public_html folder you can view it in your web browser with the following url:
localhost/~USER/index.html
Allow php to run in your public_html folder by editing your php.conf file ( I am running on php 7.4 therefore php7.4.conf)
~$ sudo nano /etc/apache2/mods-enabled/php7.4.conf
Follow the instructions and comment everything between the IfModule tags so that it looks like this:
# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>Then save the file.
Restart apache2 again to set the configuration
~$ sudo systemctl restart apache2
php will now work in your ~/public_html directory
Set up the php files and directories
cd into ~public_html and create two directories, one to receive uploaded files, and one to provide files for download
~$ cd ~/public_html
~$ mkdir -p phup
~$ mkdir -p phdn
Create the upload.php file to be able to send files from your device to your computer:
~$ nano upload.php
Add the following:
<?php
// Simple PHP script to save a file.
$dir = "phup/";
$fileName = $_REQUEST['fname'];
$data = file_get_contents('php://input');
$store = file_put_contents($dir.$fileName, $data);
if ($store !== false ) {
echo "File $fileName saved to server, $store bytes\n";
} else {
echo "Error: file $fileName not saved\n";
}
?>Save the file.
You would use a url something like this to upload a file
192.168.1.78/~USER/upload.php?fname=myfile.txt and POST the file
where the IP address is the local IP address of your computer
Create the get file list php:
~$ nano getdirs.php
Add the following:
<?php
// Simple PHP script to list directories.
$a = json_encode(array_slice(scandir("phup/"),2));
$b = json_encode(array_slice(scandir("phdn/"),2));
echo "[".$a.",".$b."]";
?>Here you get a list of the files in both directories and return a stringified JSON list
Save the file
You would use a url something like this to call the getdirs.php script:
192.168.1.78/~USER/getdirs.php
Create the App
I built my app using App Inventor and set it up to run on Android 11 devices
I won't go into detail here, because the app I wrote is quite complicated, due to the need to fetch files from all sorts of different directories on the device. Suffice to say, I used three web components, one to upload, one to fetch the directory listings, and one to download files. Upload and Download procedures were for one file at a time.
Currently it works, but I am waiting to see what limitations are applied to file location access when App Inventor targets API 30 ....