如何使用php从数据库/文件夹下载文件


How to download file from database/folder using php

上传的文件将存储在数据库和文件夹中(文件夹名称:uploads)。现在我想知道如何从数据库/文件夹创建下载文件。我有两个文件,分别是donload.php和download.php。当用户点击单词"donload"时,会出现弹出框并保存文件。

butangDonload.php

<?php
    $file = "Bang.png"; //Let say If I put the file name Bang.png
    echo "<a href='download.php?nama=".$file."'>donload</a> ";
?>

download.php

<?php
    $name= $_GET['nama'];
    download($name);
    function download($name) {
        $file = $nama_fail;
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }
?>

我已经更改了您的代码,只需少量修改即可正常工作。这是代码:

丁烷卸载.php

<?php
$file = "logo_ldg.png"; //Let say If I put the file name Bang.png
echo "<a href='download1.php?nama=".$file."'>download</a> ";
?>

下载.php

<?php
$name= $_GET['nama'];
    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename='"" . basename($name) . "'";");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($name));
    ob_clean();
    flush();
    readfile("your_file_path/".$name); //showing the path to the server where the file is to be download
    exit;
?>

在这里,您需要显示要下载文件的路径。即只给出文件名,但需要给出读取该文件的文件路径。因此,它应该被替换为我已经通过使用你的代码进行了测试,修改也会起作用。

以下是下载文件的代码

<?php
$ch = curl_init();
$downloadFile = fopen( 'file name here', 'w' );
curl_setopt($ch, CURLOPT_URL, "file link here");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 65536);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'downloadProgress'); 
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt( $ch, CURLOPT_FILE, $downloadFile );
curl_exec($ch);
curl_close($ch);
function downloadProgress ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size) {
    if($download_size!=0){
    $percen= (($downloaded_size/$download_size)*100);
    echo $percen."<br>";
}
}
?>

您可以使用html5标签直接下载图像

<?php
$file = "Bang.png"; //Let say If I put the file name Bang.png
echo "<a href='download.php?nama=".$file."' download>donload</a> ";
?>

有关详细信息,请查看此链接http://www.w3schools.com/tags/att_a_download.asp

butangDonload.php
$file = "Bang.png"; //Let say If I put the file name Bang.png
$_SESSION['name']=$file;    

试试这个,

<?php
$name=$_SESSION['name'];
download($name);
function download($name){
$file = $nama_fail;
?>
You can also use the following code:
<?php
$filename = $_GET["nama"];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename='"" . basename($filename) . "'";");
readfile("your file uploaded path".$filename);
exit();
?>
function downloadFiles($dir, $file) {
    header("Content-type: application/force-download");
    header('Content-Disposition: inline; filename="' . $dir . $file . '"');
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: " . filesize($dir . $file));
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $file . '"');
    readfile("$dir$file");
}
$dir = "folder_path";
$file = "image_name.jpg";
$download = downloadFiles($dir,$file);

试试这种方法,它对我有帮助。