Accueil > > > CLASS D'ENSEMBLE DE FICHIERS À TÉLÉCHARGER
CLASS D'ENSEMBLE DE FICHIERS À TÉLÉCHARGER
Information sur la source
Description
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
- <?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;
- }
-
- ?>
-
<?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;
}
?>
Historique
- 24 février 2005 02:04:12 :
- 24 février 2005 23:21:46 :
- Suite à une erreur d'inattention de ma part et le retour au PHP après un long moment de programmation en C++.
Corrections :
* Suppression du constructeur vierge
* Nouveau nom de fonction pour startDownloadInd($index)
- 24 février 2005 23:27:22 :
- 25 février 2005 17:31:27 :
- 25 février 2005 20:31:31 :
- 27 juillet 2005 15:32:46 :
- Nouvelle fonction d'ajout de Mime type qui ne font pas partie intégrante du constructeur
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko [FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|