想要建立像下载这样的链接吗?id = **

I wanted to make download links in php.

For an example If i have 2 files in my directory then How do i make the download links like download.php?id=1 or 2 or anything.

So what will be the codes in the download.php file.

Please somebody tell me the codes.

use $_GET

example:

<a href="download?id=1">link</a>

<?php

$id=$_GET['id'];

if(is_numeric($id)){
$filename = 'file.pdf'; // of course find the exact filename....        
header( 'Pragma: public' ); // required
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private', false ); // required for certain browsers 
header( 'Content-Type: application/octet-stream' );

header( 'Content-Disposition: attachment; filename="'. basename($filename) . '";' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: ' . filesize( $filename ) );

readfile( $filename );

exit;

}

?>

I used something like this:

$fileId = $_GET['id'];

// do something with that id and return the file path / name

$path = ""; //server path / file name    
header("Content-Type: application/octet-stream");    //    
header("Content-Length: " . filesize($path));        
header('Content-Disposition: attachment; filename='.$path);    
readfile($path); 

Database IDs

To make it possible to link download using ?id=xx, you will need database table, that associates database ids and file names.

Resolving $_GET

In php, this is how you can get ID sent by user:

$id = (int)$_GET["id"];
/*ADD YOUR GET FILE NAME CODE HERE*/
header("Content-Disposition: attachment; filename=$filename");
header("Content-Size: ".filesize($filename));
readfile($filename);   //Send file to user