Class d'ensemble de fichiers à télécharger

Contenu du snippet

Cette classe permet d'instancier des objets représentant un
ensemble de fichiers qui pourront être téléchargés.
L'utilisation est assez intuitive :
- Instancier un objet
- ajouter des fichiers à l'ensemble par la méthode
"addFile(nom_du_fichier)"
- supprimer des fichiers par la méthode
"removeFile(nom_du_fichier)"
- supprimer tous les fichiers par la méthode
"removeFiles()"
- démarrer un téléchargement par la méthode
"startDownload(nom_du_fichier)" ou "startDownloadIndex(index)"

Cette classe peut être utile lorsqu'on a une liste de fichiers dans une base
de données et que l'on récupère les noms de ceux-ci par une fonction SQL
dont le retour est généralement un tableau.

Source / Exemple :


<?php
/**

  • Project:
  • File: fmk_download_inc.php
  • Class: Download
*
  • This class makes you able to create a downloadable object that
  • contains a set of downloadable files and start the download of a
  • specific one.
*
  • @author jean_poldeux <jean_poldeux@hotmail.com>
  • @date December 20th, 2004
  • Updated July 26th, 2005
  • @version 0.1
  • /
class Download { /**
  • Basic constructor that initialises mimeTypes allowed
  • /
function Download() { $this->filenames=array(); $this->types=array(); $this->mime=array( array(".htm","text/html"), array(".html","text/html"), array(".txt","text/plain"), array(".gif","image/gif"), array(".jpg","image/jpeg"), array(".zip","application/zip"), array(".pdf","application/pdf"), array(".ppt","application/mspowerpoint"), array(".xls","application/excel"), array(".doc","application/msword"), array(".exe","application/octet-stream") ); } /**
  • Add a file to the current set
  • @param $filename : File Name that can be downloaded (STRING)
  • @return : Boolean value = TRUE if the has correctly been added - FALSE if it isn't a file, the file doesn't exist
  • or is already in the set
  • /
function addFile($filename) { //Check if it is really a file and if it exists on the disk if (is_file($filename) && file_exists($filename)) { //Check if the file is not already in if(!in_array($filename,$filenames)) { //add it to the file set array_push($this->filenames,$filename); $extension=substr($filename,strrpos($filename,'.')+1); //Check the type of download required and add it to the set for($i=0;$i<=count($this->mime);$i++) { if (strcmp($extension,$this->mime[$i][0])==0) { array_push($this->types,$this->mime[$i][1]); return true; } } return false; } else return false; } else return false; } /**
  • Remove a specific file from the set given by the index number
  • @param $index : index value of the file that has to be deleted (INTEGER)
  • @return : Boolean value = TRUE if it has correctly been removed - FALSE if the index is out of bounds or not number
  • /
function removeFile($index) { if(is_int($index) && $index>0 && $index<=count($this->filenames)) { array_splice($this->filenames,($index-1),1); array_splice($this->types,($index-1),1); return true; } else return false; } /**
  • Remove all files from the set
  • /
function removeFiles() { array_splice($this->filenames,0); array_splice($this->types,0); } /**
  • Get the number of files that are contained in the set
  • @return : Number of files in the set (INTEGER)
  • /
function getNbFiles() { return count($this->filenames); } /**
  • Add a new mime type to the default one
  • @param $extension : extension of the new file type (STRING)
  • @param $handling_method : download method which depends on the new file type (STRING)
  • @return : Boolean value = TRUE if it has correctly insered - FALSE if the extension already exists
  • /
function addMimeType($extension, $handling_method) { //Add a dot before file extension if there isn't one if(strcmp(substr($extension, 0, 1),".") != 0) { $extension = ".".$extension; } //Check if the extension doesn't already exists in this object if(array_key_exists($extension, $mime) { return false; } else { $this->mime[$extension] = $handling_method; } } /**
  • Start the download of specific file given by the index number
  • @param $index : (INTEGER) index value of the file that has to be downloaded
  • @return : Boolean value = TRUE if it has correctly started - FALSE if the index is out of bounds or not number
  • /
function startDownloadIndex($index) { if(is_int($index) && $index>0 && $index<=count($this->filenames)) { $file=basename($this->filenames[$index-1]); header("Content-disposition: attachment; filename=".$this->filenames[$index-1]); header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: ".$this->types[$index-1]."\n"); header("Content-Length: ".filesize($this->filenames[$index-1])); header("Pragma: no-cache"); header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public"); header("Expires: 0"); readfile($this->filenames[$index-1]); return true; } else return false; } /**
  • Start the download of specific file given by the filename
  • @param $filename : (STRING) Name of the file that has to be downloaded
  • @return : Boolean value = TRUE if it has correctly started - FALSE if the index is out of bounds or not number
  • /
function startDownloadFile($filename) { $index=array_search($this->filenames) if($index) { $file=basename($this->filenames[$index-1]); header("Content-disposition: attachment; filename=".$this->filenames[$index-1]); header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: ".$this->types[$index-1]."\n"); header("Content-Length: ".filesize($this->filenames[$index-1])); header("Pragma: no-cache"); header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public"); header("Expires: 0"); readfile($this->filenames[$index-1]); return true; } else return false; } /**
  • @var Set of files names (STRING INDEXED ARRAY)
  • /
var $filenames; /**
  • @var Set of download method. It depends on the file type (STRING INDEXED ARRAY)
  • /
var $types; /**
  • @var Set of mime types that are allowed (STRING INDEXED ARRAY)
  • /
var $mime; } ?>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.