1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
class Fichier
{
private $id;
private $nomFichier;
private $newNomFichier;
private $domaine;
private $tailleFichier;
private $fichierTmp;
private $fichierType;
private $fichierExtension;
private $dossierCourrant;
private $cheminTelechargement;
private $fichierExtensionAutorise = array('jpeg','jpg','png','pdf','doc');
public static $TAILLE_MAX_AUTORISEE = 500000;
public static $DOC_DEVIS = "DEVIS";
public static $DOC_BON_COMMANDE = "BC";
public static $DOC_FICHE_TRAVAIL = "FT";
public static $DOC_BON_LIVRAISON = "BL";
public static $DOC_FACTURE = "FACTURE";
public static $DOC_AVOIR = "AVOIR";
public static $DOC_PROFORMA = "PF";
public static $DOC_FIN_CHANTIER = "FC";
public function setNewNomFichier($newNomFichier)
{
$this->newNomFichier = $newNomFichier;
}
/**
*
* @param string $nom nom du fichier
* @param string $fichier fichier $_FILE
* @param string $path route du dossier
*/
public function __construct($domaine,$fichier = null,$path,$identifiant=null,$nomfichier=null,$extension = null)
{
$this->domaine = $domaine;
$this->nomFichier = $nomfichier;
$this->id = $identifiant;
$this->cheminTelechargement = getcwd()."/$path/".basename($this->nomFichier);
// $this->tailleFichier = filesize($this->nomFichier);
if($extension == null)
{
$this->fichierExtension = strtolower(end(explode('.',$this->nomFichier)));
}
else
{
$this->fichierExtension = $extension;
}
/**
* Creation du nom de fichier
*/
if($this->id != null)
{
$nbrFichier = count($this->rechercheFichier($path,$this->nomFichier));
if($nbrFichier < 1)
{
$this->newNomFichier = $this->creerNomFichierFacture($this->id,$this->fichierExtension);
}
}
else
{
$this->nomFichier = $fichier['name'];
$this->tailleFichier = $fichier['size'];
$this->fichierTmp = $fichier['tmp_name'];
$this->fichierType = $fichier['type'];
}
}
/**
*
* @param string $identifiant (numéro de facture ou docflow)
* @param string $extension (ex .pdf)
* @return string
*/
public function creerNomFichierFacture($identifiant,$extension)
{
$date = date("Ymd");
return $date."_".$identifiant."_".date("His").".".$extension;
}
public function tailleIsValide()
{
if($this->tailleFichier <= self::$TAILLE_MAX_AUTORISEE)
{
return false;
}
return true;
}
public function extensionIsValide()
{
if(!in_array($this->fichierExtension,$this->fichierExtensionAutorise)) {
return false;
}
return true;
}
/**
* Enregistrement du fichier
* return path du fichier
*/
public function deplacerFichier()
{
if( $this->extensionIsValide())
{
var_dump($this->nomFichier);
var_dump( "exec/".$this->newNomFichier);
return copy($this->nomFichier, "exec/".$this->newNomFichier);
}
}
public function fichierExist()
{
if(file_exists($this->cheminTelechargement))
{
return true;
}
return false;
}
/**
*
* @param string $path
* @param string $nomFichier
* @return array tableau contenant nom des Fichier
*/
public function rechercheFichier($path,$nomFichier)
{
if($nomFichier !== null)
{
$nom = explode("_", $nomFichier);
if(count($nom) < 3)
{
return glob($path."/".$nomFichier);
}
else
{
return glob($path."/".$nom[0]."_".$nom[1]."_*");
}
}
return null;
}
public function renommerFichier()
{
rename($this->cheminTelechargement.$this->nomFichier);
}
public function afficherFichier()
{
header("Content-type:{$this->fichierType}");
header("Content-Length: " .$this->tailleFichier);
header("Content-Description:inline;filename=".$this->cheminTelechargement.$this->nomFichier);
header('Content-Transfer-Encoding:binary');
header('Accept-ranges:bytes');
@readfile($this->cheminTelechargement.$this->nomFichier);
}
public function getNewNomFichier()
{
return $this->newNomFichier;
}
}
$f2 = new Fichier("facture",null,"exec","Identifiant_Adrien","c:\Utilisateurs\jBross\Documents/Identifiant_Adrien.pdf");
$f2->deplacerFichier();
$f2->afficherFichier(); |
Partager