As we stated in our last blog post about V1.1, there’s an extra feature that requires its own post to explain what it’s all about, due to its current state it is for medium to advanced users only.
Sending links to the phone is nice, and sending texts can make it easier to write something on your computer or grab some notes and then you’ll have it quickly in the clipboard of your phone.
But there is another feature that can make things easier sometimes…
What about sending files?
Yep, don’t you wish sometimes to being able to quickly send a file or an .apk to your phone without the need to plugin the USB cable or swap out the MicroSD card?
Of course there are other options available, the nice thing is trying to provide alternatives because some people might not want to register with a new company, or you don’t want to use a closed source app, or whatever. We think sending files is a natural progression by integrating this option with FoxToPhone, so this version includes a first experimental version.
How does it work?
First of all: this is disabled by default.
Secondly you just need three things to enable sending files.
- The latest version of FoxToPhone (V1.1)
- Write access to a web server.
- The PHP script below.
You have to set up your own web hosting server and adjust the configuration of the extension before being able to send any file, you can find the full steps a little further down.
The system is quite simple:
You setup a hosting server, accessible to both your computer and your phone (any web hosting service should be suitable), then you can drag&drop a file or folder into the toolbar button, or right click on it and pick the files that you want to send.
The server sends back a url to retrieve the upload, and that url is sent to the phone using the same method FoxToPhone normally sends links.
When the url arrives at the phone, it launches the browser with that URL, detects that it’s a file and starts the download.
Why isn’t it enabled?
Quite simply, Bandwith and Privacy.
There are already lots of people (over 20,000) using this extension, if just a few of them try to send a few hundred Kb or some Mb files, then our servers will go over quota with our hosting provider, so we need to find a proper hosting before everybody can use it. As we have built this extension as our hobby, we cannot afford the potentially massive bandwidth.
And privacy: even if the files are automatically deleted, they are sent through an external server, so people might expect some privacy about the data that they send so we are not going to enable it until we can be sure that everything is OK in that aspect.
Let me enable it for myself!
Ok, here we go. You can use any kind of server, the example script is done in PHP, but you can easily adjust it to any other server language. Remember this is an open source project, and if you do implement a version of this script in another language we only ask that you reference us. However it would be great if you choose to provide it back to the community too.
Save this code as foxtophone.php on your webserver:
<?php
// Folder to store the files
// Adjust it to your server
$filesFolder = '/home/ ... /files/';
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
if (isset($_GET['name']) && isset($_GET['type']) && isset($_GET['data']) )
{
$fileName = $_GET['name'];
$mime = $_GET['type'];
$data = $_GET['data'];
// Safety check
$data = preg_replace('/[^\dA-F]/i', '', $data);
}
else
{
echo 'Invalid request';
exit(1);
}
$file = $filesFolder.$data;
//echo $file;
if (!file_exists($file) || !is_file($file))
{
echo 'Invalid file';
exit(1);
}
ob_end_clean();
header('Connection: close');
header('Content-Encoding: none');
ob_start();
header('Content-Type: '.$mime);
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename="'.str_replace('"', '', $fileName).'"');
// Flush buffers
while (@ob_end_flush());
// Send the file
$fp = fopen($file,'r') ;
while (! feof($fp)) {
$buff = fread($fp,4096);
print $buff;
}
fclose($fp);
// Send the data and flush buffers
while (@ob_end_flush());
flush();
ob_end_clean();
// Delete it and exit
// The phone makes two requests with GET, they both seem identical, and if we delete the file
// on the first access then the real download will fail if it's a small file
// Have to file a bug about this.
// Let's wait a little so the second request starts and the file is sent there
sleep(30);
unlink($file);
exit();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_FILES['upload']))
{
$file = $_FILES['upload'];
// Be careful about all the data that it's sent!!!
// Check that the user is authenticated, that the file isn't too big,
// that it matches the kind of allowed resources...
$name = $file['name'];
$mime = $file['type'];
// create a unique filename based on IP and real filename
$data = md5($_SERVER['REMOTE_ADDR'] . $name . date('H:i:s'));
$sFilePath = $filesFolder.$data;
move_uploaded_file( $file['tmp_name'], $sFilePath ) ;
header('Content-Type: text/xml');
echo '<?xml version="1.0" standalone="yes"?>';
echo '<result>';
echo '<upload>';
// Url to retrieve the uploaded file.
echo curPageURL().'?data='.$data.'&type='.urlencode($mime).'&name='.urlencode($name);
echo '</upload>';
echo '</result>';
}
else
die('No file has been sent');
}
// Get the current page url
function curPageURL() {
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
You have to put a proper path in $filesFolder to a folder in the server where you have set write permissions in order to store the files.
Now in Firefox type “about:config” into the address bar and press enter. Edit the preference “extensions.sendtophone.fileServerUrl”, putting there the url (something like http://192.168.0.5/foxtophone.php or http://yourdomain.com/foxtophone.php, remember that if you try to use “localhost” the phone will look at itself and not your server).
And you’re ready to go.
Open the context menu of the toolbar and you’ll find two new entries to send files or folder, pick something and it will be sent to your phone.
Please, don’t be harsh on the PHP coding style, I can count with the fingers of my hands the number of PHP pages that I’ve ever written and remember that this is just experimental stuff that isn’t enabled by default.
If you want to repost something about this feature, please keep in mind that this is not meant for the average user, they don’t have a web server at hand so publishing this as an announcement for everyone can lead to confusion because they can’t make it work. We will try to find a simple and secure file hosting solution for everyone and we would love to hear your suggestions about it.