download fisiere doc cu php

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
patricia
Mesaje: 82

download fisiere doc cu php

buna,
Am nevoie sa implementez un lucru si nu stiu cum, am nevoisa sa pastrez intr-un tabel informatiile despre carti, iar pentru fiecare am nevoie sa am asociat un fisier doc care sa fie downloadabil de catre user, cum se poate face, sa zicem ca in tabel pastrez calea catre fisierul de descarcat, dar cum fac sa poata fi descarcat de user?

MarPlo Mesaje: 4343
Salut
Functia downloadFile() din codul urmator poate fi utilizata pentru descarcare mai multe tipuri de fisiere cu php (doc, jpg, zip, csv, xls, ...).
Se adauga acest cod intr-un "fisier.php", iar link-ul de descarcare trebuie sa apeleze acest fisier php, cu numele si extensia fisierului care trebuie descarcat in adresa URL, sa fie de forma asta:
fisier.php?file=nume_fisier.ext

Cod: Selectaţi tot

<?php
// function to download files with php ( https://coursesweb.net/ )
// receives the path and filename to download
function downloadFile($file) {
  $ar_ext = explode('.', $file);

  switch (strtolower(end($ar_ext))) {
    case 'pdf': $ctype = 'application/pdf'; break;
    case 'exe': $ctype = 'application/octet-stream'; break;
    case 'zip': $ctype = 'application/zip'; break;
    case 'docx': $ctype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
    case 'doc': $ctype = 'application/msword'; break;
    case 'csv': $ctype = 'text/csv'; break;
    case 'xls': $ctype = 'application/vnd.ms-excel'; break;
    case 'xlsx': $ctype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
    case 'ppt': $ctype = 'application/vnd.ms-powerpoint'; break;
    case 'gif': $ctype = 'image/gif'; break;
    case 'png': $ctype = 'image/png'; break;
    case 'jpeg': $ctype = 'image/jpg'; break;
    case 'jpe': $ctype = 'image/jpg'; break;
    case 'jpg': $ctype = 'image/jpg'; break;
    case 'swf': $ctype = 'application/x-shockwave-flash'; break;
    case 'tif': $ctype = 'image/tiff'; break;
    case 'tiff': $ctype = 'image/tiff'; break;
    case 'psd': $ctype = 'image/psd'; break;
    case 'bmp': $ctype = 'image/bmp'; break;
    case 'ico': $ctype = 'image/vnd.microsoft.icon'; break;
    case 'xml': $ctype = 'application/xml'; break;
    case 'xhtml': $ctype = 'application/xhtml+xml'; break;
    default: $ctype = '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>";
  }
}

// Usage
$dir = 'folder_with_files/';
if (isset($_GET['file'])) {
  $file = $dir . strip_tags($_GET['file']);
  downloadFile($file);
} 
La $dir se adauga directorul unde sunt fisierele ce trebuie descarcate.
Daca in adresa URL se adauga si directorul unde e fisierul de descarcat (calea completa), de exemplu:
fisier.php?file=director/nume_fisier.ext

se pune: $dir = ''; (gol).