The main topic of this post is how to make a system for download any type of file that you give access to download from your website by using php. It is very easy to make. Just keep in mind, that the file name must be stored in database with its extension name and the file must be in a folder of of your project.
So lets make this. I am showing you as a example of image download so before getting started make a table in database and insert images name with it’s extension name in the table and insert those images in a folder. If you don’t know how to make this click here to learn how to insert files in database.
I am providing the code I have used in my view page where all the images are there and under each of the image has a download button. All the images are comming from database by mysqli_fetch_assoc(my query). See this code below:
see_images.php
<?php require_once('db.php'); ?> <html> <head> <style> div.gallery { margin: 5px; border: 1px solid #ccc; float: left; width: 180px; } div.gallery:hover { border: 1px solid #777; } div.gallery img { width: 100%; height: 127px; } div.desc { padding: 15px; text-align: center; } .btn_style{ background-color: red; height: 28px; width: 77px; color: white; border-bottom-style: outset; border-bottom-left-radius: 25px; border-top-right-radius: 29px; } </style> </head> <body> <?php $qry = mysqli_query($con,"SELECT * from `file_list` order by id DESC"); while($data = mysqli_fetch_assoc($qry)){ ?> <div class="gallery" id="my_image<?php echo $data['id']; ?>"> <a href="commentimage.php?img_id=<?php echo base64_encode($data['id']); ?>"> <img src="my_file/<?php echo $data['file_name']; ?>" alt="Fjords" width="300" height="200"> </a> <div class="desc"><button class="btn_style" onclick="delete_image(<?php echo $data['id']; ?>)">Delete</button></div> <button onclick="download_this('<?php echo $data["file_name"]; ?>')">Download</button> </div> <?php } ?> </body> </html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script> function delete_image(id){ $.ajax({ type: 'post', url: 'delete_image.php', data: {image_id:id}, success: function(result){ $('#my_image'+id).fadeOut(); } }); } function download_this(img_name){ window.location.href="image_download.php?filename="+img_name; //alert(img_name); } </script>
You can see I have redirected to image_download.php page with filename. And I will receive this file name in image_download.php page and make the downloading code there. See this :
image_download.php
<?php $filename='my_file/'.$_GET['filename']; $attachment_location = 'my_file/'.$_GET['filename']; if (file_exists($attachment_location)) { header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Cache-Control: public"); // needed for internet explorer header("Content-Type: application/zip"); header("Content-Transfer-Encoding: Binary"); header("Content-Length:".filesize($attachment_location)); header("Content-Disposition: attachment; filename=".$attachment_location); readfile($attachment_location); die(); } else { die("Error: File not found."); } ?>
Hope this article will be help full to you. Subscribe for more updates.