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
QUELQUES TRUCS INTéRESSANTS (05/09/2010)QUELQUES TRUCS INTéRESSANTS (05/09/2010) par coq
Cette fois-ci : .NET Debug / Performance Sécurité SQL Server .NET Determining if a type is defined in the .NET Framework (blog de Scott Dorman) Ha tiens, je n'avais jamais vraiment pensé à utiliser le jeton de clé publique...
Cliquez pour lire la suite de l'article par coq ENUMERABLECOLLECTIONENUMERABLECOLLECTION par Matthieu MEZIL
Prenons le scénario suivant. On utilise MVVM. On a les deux classes suivantes dans le model : public class Child { } public class Parent { private ObservableCollection < Child > _children; public ObservableCollection < Child > Children { get {...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [HS] CHROME 6 + MOI = COUP DE GUEULE ![HS] CHROME 6 + MOI = COUP DE GUEULE ! par JeremyJeanson
Attention, le poste qui suit n'est pas la complainte d'une personne : Qui n'aime pas Chrome. D'un anti Google. D'un développeur qui a un poil énorme dans la main. Ceux qui me fréquentent savent que je change de navigateur favori tous les 2 ou 3 mois afin ...
Cliquez pour lire la suite de l'article par JeremyJeanson [WP7] UTILISER UN WRAPPANEL DANS UNE APPLICATION WINDOWS PHONE 7[WP7] UTILISER UN WRAPPANEL DANS UNE APPLICATION WINDOWS PHONE 7 par Audrey
Lors de la réalisation de ma 2ème application Windows Phone 7, j'ai souhaité utiliser un WrapPanel pour afficher plusieurs photos. Mais le contrôle WrapPanel ne fait pas parti de la liste des contrôles inclus dans le SDK de la version Beta des outils pour...
Cliquez pour lire la suite de l'article par Audrey [WP7] BESOIN D'AVOIR DES DONNéES EN CACHE[WP7] BESOIN D'AVOIR DES DONNéES EN CACHE par Nicolas
Les développeurs ASP.NET ont l'habitude de mettre des données en cache pour éviter de requêter a chaque fois la base de données. Et il est toujours utilie de penser que vos utilisateurs mobiles n'ont pas troujours une super connexion 3G/WIFI et un for...
Cliquez pour lire la suite de l'article par Nicolas
Logiciels
WebLogAndPass (1.0.0)WEBLOGANDPASS (1.0.0)WebLogAndPass est un logiciel permettant de mémoriser vos sites préférés et pour chacun d'entre-e... Cliquez pour télécharger WebLogAndPass uTorrent (2.0.4)UTORRENT (2.0.4)C'est un client BitTorrent très puissant et très performant. Comme son nom l'indique, uTorrent (m... Cliquez pour télécharger uTorrent Bureau de Gestion - ERP Devis Facturation (2.02)BUREAU DE GESTION - ERP DEVIS FACTURATION (2.02)- Version gratuite du 10/06/2010
Le Bureau de Gestion est un logiciel dédié à la gestion de l'en... Cliquez pour télécharger Bureau de Gestion - ERP Devis Facturation 4Videosoft Transfert iPod Mac (3.2.08)4VIDEOSOFT TRANSFERT IPOD MAC (3.2.08)4Videosoft Transfert iPod-Mac caractérise principalement à transférer les fichiers iPod vers Mac.... Cliquez pour télécharger 4Videosoft Transfert iPod Mac 4Videosoft HD Convertisseur (3.3.08)4VIDEOSOFT HD CONVERTISSEUR (3.3.08)Etant le meilleur HD Vidéo Convertisseur, 4Videosoft HD Convertisseur, vous pouvez regarder la vi... Cliquez pour télécharger 4Videosoft HD Convertisseur
|