Coding download file dengan php

  1. Home
  2. PHP MYSQLi
  3. Script download file dengan HTML5 dan php

Halo , ,,, teman-teman.. ada yang bingung bagaimana membuat script download file dengan php..??? sebenarnya untuk membuat download file , admin sudah jelaskan di artikel upload file dengan php mysqli

namun sekarang admin akan jelaskan atau contohkan lagi pada file pdf.. silahkan di simak ya

Download File dengan atribut HTML5

Download File

 Jika kita ingin mengganti nama file, dengan HTML5 kita bisa menambahkan 

Download File

Script Download File dengan PHP

contoh link download  ;

Download File

download.php

if (isset($_GET['file'])) {
$file = $_GET['file'];
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
}
?>

Artikel Rekomendasi

Artikel Terkait Script PHP upload dan Simpan gambar di database mysqliMembuang spasi di awal, tengah, dan akhir string di PHPKonversi Bilangan dengan PHPMultiple Insert, Update, Delete dengan PHP & MySQLi OOPSensor Nomor HP dengan PHPGambar tidak Tersimpan Saat Upload FileFree Aplikasi Ujian Online dengan PHP MYSQLMenampilkan Data Terakhir Di Group ByQuery Menghitung Field Berdasarkan Group di PHP MYSQLPenjumlahan Pengurangan Perkalian dan Pembagian PHP

Diskusi

This short tutorial will help you to learn how to download a file with PHP.

Just follow the examples below to easily meet that goal.

If you want to make different types of files or images load the files directly into the drive of the user with PHP, you can run the readfile() function.

Let’s see how to do it on the example of creating an image gallery, which will help the users to download image files using just one click.

In the example below, an image-gallery.php is generated and a code is placed inside it:

html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Simple Image Gallerytitle>
    <style>
      .img-box {
        display: inline-block;
        text-align: center;
        margin: 0 15px;
      }
    style>
  head>
  <body>
    <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) . '">Downloada>p>';
        echo 'div>';
    }
    ?>
  body>
html>

So, in the example above, the download link points to the download.php file. The URL, on its turn, encompasses an image file name, just as a query string. Also, you can notice, that the urlencode() function is applied for encoding the image file names in a way that they may be safely passed like a URL parameter. The reason is that file names may include unsafe URL characters. The entire code of the download.php, forcing image download looks as follows:


if(isset($_REQUEST["file"])){
    // Get parameters
    $file = urldecode($_REQUEST["file"]); // Decode URL-encoded string

    /* Check if the file name includes illegal characters
    like "../" using the regular expression */
    if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
        $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);
            die();
        } else {
            http_response_code(404);
	        die();
        }
    } else {
        die("Invalid file name!");
    }
}
?>

Other file formats such as pdf, doc, and so on, can also be downloaded in the way, demonstrated above.

It is crucial to consider that in the example above, the regular expression (line 8) doesn’t allow files with the names that start or end with a dot (.). For example, you can use filenames such as books.jpg or Books.jpg but can’t use books.jpg., .kites.jpg, and so on.