问题
I have a physical files which I want users to download on my website. The files are located at:
C:/xampp/htdocs/myfile/uploads/*
I need a PHP script which can download files dynamically. Assuming I have the following link in my website which when clicked, will execute a script
<a href="magic.php"> Download file </a>
What PHP code do I need in the magic.php
to download a file from the above path?
回答1:
Download link
<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>
magic.php page
<?php
$file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
回答2:
Maybe you could do something like this: (Correct me if i am wrong)
<a href="uploads/<?php echo $row['name']; ?>" download="download">
来源:https://stackoverflow.com/questions/27668971/downloading-a-file-from-a-php-server-via-a-website