How to download file from database/folder using php

后端 未结 5 1819
情书的邮戳
情书的邮戳 2020-12-14 12:21

File uploaded will be store in database and folder (folder name: uploads).Now I want to know how to create download file from database/folder. I have two file butangDonload.

相关标签:
5条回答
  • 2020-12-14 12:36
    butangDonload.php
    
    $file = "Bang.png"; //Let say If I put the file name Bang.png
    $_SESSION['name']=$file;    
    

    Try this,

    <?php
    
    $name=$_SESSION['name'];
    download($name);
    
    function download($name){
    $file = $nama_fail;
    ?>
    
    0 讨论(0)
  • 2020-12-14 12:40

    here is the code to download file with how much % it is downloaded

    <?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>";
    
    }
    
    }
    ?>
    
    0 讨论(0)
  • 2020-12-14 12:43

    I have changed to your code with little modification will works well. Here is the code:

    butangDonload.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> ";
    ?>
    

    download.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;
    ?>
    

    Here you need to show the path from where the file to be download. i.e. will just give the file name but need to give the file path for reading that file. So, it should be replaced by I have tested by using your code and modifying also will works.

    0 讨论(0)
  • 2020-12-14 12:47

    You can use html5 tag to download the image directly

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

    For more information, check this link http://www.w3schools.com/tags/att_a_download.asp

    0 讨论(0)
  • 2020-12-14 12:51
    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();
    ?>
    
    0 讨论(0)
提交回复
热议问题