Downloading a file from a PHP server via a website [closed]

空扰寡人 提交于 2020-06-16 03:48:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!