How to Download File Using PHP

How to Download File Using PHP


Normally, you do not essentially got to use any server facet scripting language like PHP to transfer pictures, zip files, PDF documents, exe files, etc. If such quite file is keep during a public accessible folder, you'll be able to simply produce a link inform to it file, and whenever a user click on the link, browser can mechanically downloads that file.

Example
How to Force Download Files Using PHP
<a href="downloads/test.zip">Download Zip file</a>
<a href="downloads/masters.pdf">Download PDF file</a>
<a href="downloads/sample.jpg">Download Image file</a>
<a href="downloads/setup.exe">Download EXE file</a>


Clicking a link that points to a PDF or a picture file won't cause it to transfer to your Winchester drive directly. it'll solely open the get into your browser. any you'll be able to put it aside to your Winchester drive. However, nada and exe files square measure downloaded mechanically to the Winchester drive by default.

You can force pictures or alternative quite files to transfer on to the user's Winchester drive victimisation the PHP readfile() perform. Here we're reaching to produce a straightforward image gallery that enables users to transfer the image files from the browser with one depression.
Let's produce a file named "image-gallery.php" and place the subsequent code within it.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Image Gallery</title>
<style type="text/css">
    .img-box{
        display: inline-block;
        text-align: center;
        margin: 0 15px;
    }
</style>
</head>
<body>
    <?php
    // Array containing sample image file names
    $images = array("kites.jpg", "balloons.jpg");
    
    // Loop through array to create image gallery
    foreach($images as $image){
        echo '<div class="img-box">';
            echo '<img src="images/' . $image . '" width="200" alt="' .  pathinfo($image, PATHINFO_FILENAME) .'">';
            echo '<p><a href="download.php?file=' . urlencode($image) . '">Download</a></p>';
        echo '</div>';
    }
    ?>
</body>
</html>

If you see the above example code carefully, you'll find the download link pints to a "download.php" file, the URL also contains image file name as a query string. Also, we've used PHP urlencode()function to encode the image file names so that it can be safely passed as URL parameter, because file names may contain URL unsafe characters.
Here's the complete code of "download.php" file, which force image download.

ExampleDownload

<?php
if(isset($_REQUEST["file"])){
    // Get parameters
    $file = urldecode($_REQUEST["file"]); // Decode URL-encoded string
    $filepath = "images/" . $file;
    
    // Process download
    if(file_exists($filepath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filepath));
        flush(); // Flush system output buffer
        readfile($filepath);
        exit;
    }
}
?>

List of Backlinks


https://uxfol.io/mistigoyal99/about
https://www.kniterate.com/community/users/delhiford/
http://browser.geekbench.com/user/235932
http://p.booklog.jp/users/delhifordin/profile
https://buzzon.khaleejtimes.com/author/delhifordin/
https://buzzon.khaleejtimes.com/author/noidaford/
http://p.booklog.jp/users/noidaford/profile
http://browser.geekbench.com/user/235929
https://www.kniterate.com/community/users/noidaford/
https://uxfol.io/home
https://buzzon.khaleejtimes.com/author/faridabad/
http://p.booklog.jp/users/faridabad/profile
http://browser.geekbench.com/user/235917
https://www.kniterate.com/community/users/faridabadford/
http://pzhgp-tarnobrzeg.cba.pl/forum/member.php?action=profile&uid=1511
https://uxfol.io/home
https://www.abcwi.org/Activity-Feed/userId/32541
https://www.kniterate.com/community/users/ghaziabadford/
http://browser.geekbench.com/user/235910
http://en.miui.com/home.php?mod=space&uid=2260899101
http://p.booklog.jp/users/ghazaibad/profile
https://buzzon.khaleejtimes.com/author/ghaziabadford/

Comments

Popular posts from this blog

High PR Profile Creation Site Lists in 2019

Top 400+ Profile Creation Sites List 2019

Beginner's Guide to Link Building