Setting up a local Apache for HTTP-POST file uploads

Because of filesystem restrictions on the iPhone we don’t have access to our quality assessment results directly. Hence, we need a way to submit the collected data to a remote machine. HTTP seems to be the perfect protocol for this task and POST the recommended method. Using HTTP-POST we can save the data into normal files or we can even put the data into a database. However, this method requires a properly configured Webserver and a server-side script to handle the POST requests (Apache just forwards the posted data to this script). The following procedure explains a very simple and insecure setup for Apache 2 on Mac OSX. Don’t use it on publicly accessible Webservers because you will quickly end up with a rootkit installed!

1. Configure Apache 2 on OSX

Edit the file /private/etc/apache2/httpd.conf as root (sudo vi /private/etc/apache2/httpd.conf) and enable PHP support. You have to change the following lines.

# from
#LoadModule php5_module        libexec/apache2/libphp5.so

# to
LoadModule php5_module        libexec/apache2/libphp5.so
AddHandler php5-script php

2. Create the target path

To store the uploaded files in the local filesystem, create a directory and grant write permissions to the webserver. Remember to use your OSX username in the commands below. The file permissions (753) allow all users (including the webserver) to save data only.

mkdir /Users/<username>/results
chmod 753 /Users/<username>/results

3. Install the POST Handler Script

Copy your handler script to the cgi-bin directory of the webserver (/Library/WebServer/CGI-Executables). Here is an example script that works for posting single files from this HTML form. You have to adapt the script to your own purposes and save it with the name upload.php.

The example PHP script

<?php

$target_path = "/Users/<username>/results/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

echo "Source=" .        $_FILES['uploadedfile']['name'] . "<br />";
echo "Destination=" .   $destination_path . "<br />";
echo "Target path=" .   $target_path . "<br />";
echo "Size=" .          $_FILES['uploadedfile']['size'] . "<br />";

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

The HTML form

<html>
<head></head>
<body>
<h4> File uploads </h4>
<form enctype="multipart/form-data" action="/cgi-bin/upload.php"
    method="post">
<p>
Select File:
<input type="file" size="35" name="uploadedfile" />
<input type="submit" name="Upload" value="Upload" />
</p>
</form>
</body>
</html>

Restart Apache now.

4. Test the Uploading Feature

(Note: These steps are only necessary to test file upload from a Web-Browser. Upload from the iPhone does not require that.) In order to test if the upload really works, place the HTML file from above in your local Sites directory (/Users/<username>/Sites). You also have to tell Apache2 to use your user-directory (the standard Apache configuration did not work for me). For that, you have to copy the following lines into a file at /private/etc/apache2/users/<username>.conf.

<Directory "/Users/<username>/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all

Also append the following line to the Apache 2 configuration file /private/etc/apache2/httpd.conf.

Include /private/etc/apache2/users/*.conf

Restart Apache now and everything should work. Have fun.