Linux133.fzu.cz

Úvod
MyWiki
PHP
Manuály
Poznámky
Download










Administrace

Popis a zdrojový kód scriptu download.php

Script je určen pro stahování souborů z webového datového úložiště /var/wwwdata na serveru dell006e.fzu.cz. Má jen jeden parametr "file" který určuje soubor i cestou který se bude stahovat. Cesta k souboru začíná adresářem www?? uživatele. Před tuto cestu script sám automaticky přidává cestu datového úložiště aby nemohlo dojít k jeho zneužití pro stahování jiných souborů v adresářové struktuře serveru. Navíc má script blokování proti stahování souborů s koncovkami: .php, .htm a .html. V tomto případě script jen zobrazí hlášení např.: Cannot be used for.php files! V případě že požadovaný soubor nebude na dané cestě nalezen, script zobrazí chybové hlášení: 404 File not found!

Příklad volání scriptu: www.fzu.cz/library/php/download.php?file=www4/pokus.pdf

Zdrojový kód scriptu

<?php
/*
****************************************************************************************************
* Script pro bezpecne stahovani souboru z uloziste 'wwwdata' na serveru dell006e (www.fzu.cz).     *
*--------------------------------------------------------------------------------------------------*
* Pripadne dotazy smerujte na adresu: Miloslav Strunc strunc@fzu.cz                                *
* Volani scriptu: download.php?file=www4/pokusny_soubor.doc                                        *
* Parametr: file - stahovany soubor i s cestou pocinajici adresarem vlastnika souboru (napr. www4)  *
* Datum posledni modifikace: 20.7.2007                                                             *
* Opravy a zmeny:                                                                                  *
*  - zatim zadne
*/
$path "/var/wwwdata/";

$file $_GET["file"];
if (
$file{0}=="/"$file substr($file,1);
$file $path.$file;

//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }

//Gather relevent info about file
$len filesize($file);
$filename basename($file);
$file_extension strtolower(substr(strrchr($filename,"."),1));

//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
  case 
"pdf"$ctype="application/pdf"; break;
  case 
"exe"$ctype="application/octet-stream"; break;
  case 
"zip"$ctype="application/zip"; break;
  case 
"doc"$ctype="application/msword"; break;
  case 
"xls"$ctype="application/vnd.ms-excel"; break;
  case 
"ppt"$ctype="application/vnd.ms-powerpoint"; break;
  case 
"gif"$ctype="image/gif"; break;
  case 
"png"$ctype="image/png"; break;
  case 
"jpeg":
  case 
"jpg"$ctype="image/jpg"; break;
  case 
"mp3"$ctype="audio/mpeg"; break;
  case 
"wav"$ctype="audio/x-wav"; break;
  case 
"mpeg":
  case 
"mpg":
  case 
"mpe"$ctype="video/mpeg"; break;
  case 
"mov"$ctype="video/quicktime"; break;
  case 
"avi"$ctype="video/x-msvideo"; break;
  case 
"txt"$ctype="text/plain"; break;

  
//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
  
case "php":
  case 
"html":
  case 
"htm": die("<b>Cannot be used for '$file_extension' files!</b>"); break;
  default:    
$ctype="application/force-download";
  }

//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");

//Use the switch-generated Content-Type
header("Content-Type: $ctype");

//Force the download
header("Content-Disposition: attachment; filename=$filename;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $len");
@
readfile($file);
flush();
exit;

?>