Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Saving Files From A Website

Featured Replies

How do I code it so that clicking a button automatically saves the linked document to the users computer? I'm using a vcard (.vcf) and when the button is clicked it's opening as a text file in the browser. I want it to download to the computer, without having to explain to "right click >>save as" on the page.

 

Thanks

 

Ben

Create a new php file called launchdl.php, and insert the following code:

<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

Save it in the same folder as the webpage, and save your .vcf in the same folder too. In your html change the link to:

<a href="launchdl.php?file=INSERT FILE NAME HERE.vcf"></a>

Replace "INSERT FILE NAME HERE" with your filename.

 

Job done. ^_^

Don't use jacksaidwhat's code without making sure you have some security checks in there as with that you can download any file from the server.

Don't use jacksaidwhat's code without making sure you have some security checks in there as with that you can download any file from the server.

KieranA has a very good point. If you have sensitive files on your server then you should use the following for launchdl.php:

 

<?php
$file = $_GET['file'];

if(preg_match('/^[a-zA-Z0-9_\-]+.vcf$/', $file) == 0) {
print "Illegal name: $file";
return;
}

header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

Only .vcf files will be able to be downloaded, everything else will return "Illegal name: filename.whatever" ^_^

Plus one from me

 

I've used this for pdf files before

one thing i found you need to place this php file and the files you want to download in the same folder

  • Author

Hi Guys,

 

I'm getting this when trying to click the button:

 

 

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/content/j/a/m/jambo55/html/clients/lmk/launchdl.php on line 2

 

Parse error: syntax error, unexpected T_STRING in /home/content/j/a/m/jambo55/html/clients/lmk/launchdl.php on line 2

 

Any ideas?

Not entirely sure, without seeing your php file, it may be that you have copied something wrong into it.

 

I've attached all the working files and an example vcard so you can compare, hope it helps:

Vcard_download_files.zip

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.