Local links in Firefox

Juna - 8 Dec 2006

Sometimes it's useful to open local files from scripts (html, js) in Firefox, e.g. if a remote server can manage user's local files, writing something like:

<a xhref="file:///directory/file.ext">Click here</a>

With new versions, Firefox denies to use scripts like these for security reasons and when those links are clicked, it says something like: "local links cannot be opened from a remote script".

After searching for the solution online, I only found the suggestion to change the current Firefox profile's preferences by adding some lines.

As this workaround is extremely annoying and totally user-unfriendly, I found myself another one that doesn't need any user intervention on configuration.

It's possible to read a file with php and then pretend we're trying to download that file with the Header() function: e.g. we can use a php script with a GET parameter in input (test.php):

$url = $_GET['url'];
$handle = fopen($url, "r");
$contents = fread($handle, filesize($url));
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"".basename($url)."\";");
header('Content-Length: ' . filesize($url));
@readfile($url) OR die();

So when we click on a link like this:

<a xhref="test.php?url=/path/to/file/FileName.Ext">Click here</a>

the "save/preview" dialog will popup and it will be possible to preview or save the file.



gio - 21 Jun 2007 11:17


Grazie! Ma...

Giacomo - 16 Jul 2008 10:35

Grazie mille per la tua soluzione; l'ho provata anch'io ma mi fa scaricare il file php con il codice della funzione... :-P
Weblog Koan Projects Photos Contacts