Php-mysql Course

The downloadFile() function presented in this page can be used to force download various type of files with PHP: csv, doc, html, jpg, pdf, png, ppt, xls, xml, zip, etc..

The downloadFile() function

// function to download files with php ( https://coursesweb.net/php-mysql/ )
// receives the path and filename to download
function downloadFile($file) {
  $ar_ext = explode('.', $file);
  $ext = strtolower(end($ar_ext));
  $extensions = array(
    'bmp' => 'image/bmp',
    'csv' => 'text/csv',
    'doc' => 'application/msword',
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'exe' => 'application/octet-stream',
    'gif' => 'image/gif',
    'htm' => 'text/html',
    'html' => 'text/html',
    'ico' => 'image/vnd.microsoft.icon',
    'jpeg' => 'image/jpg',
    'jpe' => 'image/jpg',
    'jpg' => 'image/jpg',
    'pdf' => 'application/pdf',
    'png' => 'image/png',
    'ppt' => 'application/vnd.ms-powerpoint',
    'psd' => 'image/psd',
    'swf' => 'application/x-shockwave-flash',
    'tif' => 'image/tiff',
    'tiff' => 'image/tiff',
    'xhtml' => 'application/xhtml+xml',
    'xml' => 'application/xml',
    'xls' => 'application/vnd.ms-excel',
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'zip' => 'application/zip'
  );
  $ctype = isset($extensions[$ext]) ? $extensions[$ext] : 'application/force-download';

  if (file_exists($file) && is_readable($file)) {
    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');


    header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);    // required for certain browsers
    header('Content-Type: '. $ctype);
    header('Content-Disposition: attachment; filename='. $file .';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '. filesize($file));
    readfile($file);
  }
  else {
    header('HTTP/1.0 404 Not Found');
    echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
  }
}
Example of usage:
<?php
// HERE ADD THE downloadFile() function

$dir = 'download/';    // folder wth files for download

// $_GET['file'] contains the name and extension of the file stored in 'download/'
if (isset($_GET['file'])) {
  $file = $dir . strip_tags($_GET['file']);
  downloadFile($file);
}
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Force Download files with PHP

Last accessed pages

  1. SHA512 Encrypt hash in JavaScript (24763)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137557)
  3. Refresh page if window width changes from a device size to other (1729)
  4. innerHTML and outerHTML to Get and Replace HTML content (30502)
  5. Star shapes with CSS (11160)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (246)
  2. Read Excel file data in PHP - PhpExcelReader (84)
  3. The Four Agreements (73)
  4. PHP Unzipper - Extract Zip, Rar Archives (72)
  5. The Mastery of Love (63)
Chat
Chat or leave a message for the other users
Full screenInchide