Using the PHP command line web server to transfer files between devices on a local network

When you install PHP, you get a simple built-in webserver as a bonus. This is very handy for testing web pages you’re writing, but I also sometimes use it as a simple way to transfer files between devices on my local wifi network. In particular, it’s a convenient way of getting files from my laptop onto my phone.

The basic idea is that I run the PHP web server in the folder that contains the files I want to transfer onto my phone, then use the browser on my phone to download the files. Let’s say that the file I’m transferring is called “myfile.txt” and it’s in the directory “/home/xubuntu/Downloads/“. I would type the following commands in a terminal to run the PHP web server in that directory:

cd /home/xubuntu/Downloads
php -S 0.0.0.0:8000

The “0.0.0.0” part is a dummy IP address that tells the PHP web server to listen on all network interfaces (e.g. 127.0.0.1 and 192.168.0.16 on my machine). I’ve also specified 8000 as the port number for the web server. To download the file onto my phone, the URL would be:

http//192.168.0.16:8000/myfile.txt

The phone needs to be connected to the same local network as the computer that the web server is running on. I have both connected to my wifi router.

If I have a few files to transfer, or if the filenames are long, it can be useful to make a little PHP script in the directory to generate a simple web page with links to each file. This is what I’m currently using (saved in the same directory as “index.php“):

<!DOCTYPE html>

<head>
    <style>
        body {font-size:200%;}
    </style>
</head>

<body>
    <br>

    <?php
    $i = 1;
    $g = glob("*");
    foreach ($g as $f)
    {
        if ($f == "index.php") continue;
        echo $i . ". " . "<a href=\"" . $f . "\">" . $f . "</a><br><br>";
        $i = $i + 1;
    }
    ?>
</body>

Here are the files stored in “/home/xubuntu/Downloads/”:

And here’s how it looks in the phone browser (the address is “192.168.0.16:8000”):

When I’m finished transferring files, I just press Ctrl-C in the terminal to close down the web server.

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s