Jump to content

[SOLVED] how to download file from hyperlink?


sayedsohail

Recommended Posts

How to download file from hyperlink?

 

--------------------------------------------------------------------------------

 

hi everyone,

 

I successfully created a pdf file and stored the file name,location inside a table.

 

Now i am showing the link on the page and when user click this link i wish to open this pdf file from the server on the user desktop in a popup window.

 

 

$filelocation == http://locatiion/pdf/xyz.pdf 

i.e,  
echo "<a href='<?php $filelocation ?>'Click to open</a>";  

 

 

Can someone help, how i should achieve this?

 

Its bit scary when i read another thread about downloading big files, although my files are very tiny i mean 30kb in size.

 

--------------------------------------------------------------------------------

 

Link to comment
Share on other sites

create a download page

 

download.php?file=pdf/xyz.pdf

<?php
forceDownload($_GET['file']);

function forceDownload($archiveName) {
	$headerInfo = '';

	if(ini_get('zlib.output_compression')) {
		ini_set('zlib.output_compression', 'Off');
	}

	// Security checks
	if( $archiveName == "" ) {
		echo "<html><title>PDF - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
		exit;
	} 
	elseif ( ! file_exists( $archiveName ) ) {
		echo "<html><title>PDF - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
		exit;
	}

	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false);
	header("Content-Type: application/pdf");
	header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".filesize($archiveName));
	ini_set("memory_limit","32M");
	readfile("$archiveName");
 }
?>

 

please note this is just an example

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.