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

  1. Setup public_html

    1. 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.

    2. Open up a terminal and do the following:

      1. ~$ mkdir -p ~/public_html

      2. ~$ sudo a2enmod userdir

      3. ~$ chmod -R 755 ~/public_html

      4. ~$ sudo systemctl restart apache2

    3. 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:

      1. localhost/~USER/index.html

  2. 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)

    1. ~$ sudo nano /etc/apache2/mods-enabled/php7.4.conf

    2. 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>

    3. Then save the file.

    4. Restart apache2 again to set the configuration

      1. ~$ sudo systemctl restart apache2

    5. php will now work in your ~/public_html directory

Set up the php files and directories

  1. cd into ~public_html and create two directories, one to receive uploaded files, and one to provide files for download

    1. ~$ cd ~/public_html

    2. ~$ mkdir -p phup

    3. ~$ mkdir -p phdn

  2. Create the upload.php file to be able to send files from your device to your computer:

    1. ~$ nano upload.php

    2. 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";
      }
      ?>

    3. Save the file.

    4. You would use a url something like this to upload a file

      1. 192.168.1.78/~USER/upload.php?fname=myfile.txt and POST the file

      2. where the IP address is the local IP address of your computer

  3. Create the get file list php:

    1. ~$ nano getdirs.php

    2. 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."]";
      ?>

    3. Here you get a list of the files in both directories and return a stringified JSON list

    4. Save the file

    5. You would use a url something like this to call the getdirs.php script:

      1. 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 ....