Subversion Repositories Applications.projet

Rev

Rev 24 | Rev 249 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 ddelon 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This library is free software; you can redistribute it and/or                                        |
9
// | modify it under the terms of the GNU General Public                                                  |
10
// | License as published by the Free Software Foundation; either                                         |
11
// | version 2.1 of the License, or (at your option) any later version.                                   |
12
// |                                                                                                      |
13
// | This library is distributed in the hope that it will be useful,                                      |
14
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
15
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
16
// | General Public License for more details.                                                             |
17
// |                                                                                                      |
18
// | You should have received a copy of the GNU General Public                                            |
19
// | License along with this library; if not, write to the Free Software                                  |
20
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
21
// +------------------------------------------------------------------------------------------------------+
202 alexandre_ 22
// CVS : $Id: fichier.class.php,v 1.6 2007-04-19 09:25:50 alexandre_tb Exp $
2 ddelon 23
/**
24
* Application projet
25
*
26
* La classe fichier
27
*
28
*@package projet
29
//Auteur original :
30
*@author        Alexandre Granier <alexandre@tela-botanica.org>
31
//Autres auteurs :
32
*@author        Aucun
33
*@copyright     Tela-Botanica 2000-2004
202 alexandre_ 34
*@version       $Revision: 1.6 $
2 ddelon 35
// +------------------------------------------------------------------------------------------------------+
36
*/
37
 
38
 
39
// +------------------------------------------------------------------------------------------------------+
40
// |                                            ENTETE du PROGRAMME                                       |
41
// +------------------------------------------------------------------------------------------------------+
42
 
43
include_once PROJET_CHEMIN_CLASSES.'type_fichier_mime.class.php' ;
44
 
45
// +------------------------------------------------------------------------------------------------------+
46
// |                                            ENTETE du PROGRAMME                                       |
47
// +------------------------------------------------------------------------------------------------------+
48
 
49
 
50
/**
51
 * class fichier
52
 * Cette classe représente un fichier au sens physique du terme. Elle fonctionne
53
 * pour les système UNIX.  A faire : adaptation selon les système. L'objectif est de
54
 * gérer correctement l'upload de fichier.
55
 */
56
class fichier
57
{
58
    /*** Attributes: ***/
59
    /**
60
     * Le nom du fichier, avec son extension.
61
     * @access private
62
     */
63
    var $_nom;
64
    /**
65
     * Le chemin UNIX ou Windows pour accéder au fichier sur le serveur (par ex;
66
     * /var/www/fichier.txt
67
     * @access private
68
     */
69
    var $_chemin;
70
    /**
71
     * Cet attribut contient une valeur du type 755, indiquant les droits afférent à un
72
     * fichier selon le système UNIX (propriétaire, groupe, autres)
73
     * @access private
74
     */
75
    var $_droits_unix;
76
    /**
77
     * Le type indique si le fichier est un répertoire, un lien ou un fichier.
78
     * @access private
79
     */
80
    var $_type = 'fichier';
81
 
82
    /**
83
     * l'identifiant du type mime.
84
     * @access private
85
     */
86
    var $_type_mime;
87
 
88
    /**
89
     * Le chemin vers le fichier, en partant du répertoire de travail.
90
     * @access private
91
     */
92
    var $_prefixe_chemin;
93
 
94
    /**
95
     *
96
     *
97
     * @return void
98
     * @access public
99
     */
24 alexandre_ 100
    function fichier( $chemin, &$objetDB)
2 ddelon 101
    {
102
        $this->_chemin = $chemin ;
103
        // On analyse l'extension pour découvrir le type mime
104
        $partie_chemin = pathinfo ($this->_chemin) ;
105
 
106
        if (is_object($objetDB) && isset($partie_chemin['extension'])) {
107
            $this->_type_mime = type_fichier_mime::factory ($partie_chemin['extension'], $objetDB) ;
108
        }
109
        // calcul du type
110
        if (is_dir ($this->_chemin)) $this->_type = 'repertoire' ;
111
    } // end of member function fichier
112
 
113
    /**
114
     * Le constructeur de la classe.
115
     *
116
     * @param string chemin Le chemin du fichier sur le serveur.
117
     * @return fichier
118
     * @access public
119
     */
24 alexandre_ 120
    function __construct( $chemin, &$objetDB)
2 ddelon 121
    {
15 ddelon 122
        $this->fichier($chemin, $objetDB);
2 ddelon 123
 
124
    } // end of member function __construct
125
 
126
    /**
127
     *
128
     *
129
     * @return void
130
     * @access public
131
     */
132
    function suppression()
133
    {
134
        if ($this->_type == 'repertoire') rmdir ($this->_chemin) ;
135
        if ($this->_type == 'fichier') unlink ($this->_chemin) ;
136
    } // end of member function suppression
137
 
138
    /**
139
     * Réalise l'upload d'un fichier vers chemin_destination.
140
     *
141
     * @param string chemin_destination Il s'agit du chemin UNIX de destination du fichier. Le script doit avoir les
142
     * droits en écriture sur le répertoire.
143
     * @global    mixed une référence vers un objet HTML_QuickForm_File
144
     * @return void
145
     * @access public
146
     */
147
    function upload( $chemin_destination )
148
    {
149
        if (move_uploaded_file($_FILES['fichier']['tmp_name'], $chemin_destination)) {
150
            return true ;
151
        } else {
152
            return false ;
153
        }
154
    } // end of member function upload
155
 
156
    /**
157
     * Déplace un fichier, renvoie vrai en cas de succès.
158
     *
159
     * @param string origine L'emplacement de départ du fichier.
160
     * @param string destination Le répertoire d'arrivé.
161
     * @return bool
162
     * @access public
163
     */
164
    function deplace( $origine,  $destination )
165
    {
166
        if (copy ($origine, $destination )) {
167
            if (unlink($origine)) return true ;
168
        }
169
        return false ;
170
    } // end of member function deplace
171
 
172
    /**
173
     *
174
     *
175
     * @param int id_type_mime L'identifiant du type mime du document.
176
     * @return type_fichier_mime
177
     * @access public
178
     */
179
    function getTypeMime(  )
180
    {
181
        return $this->_type_mime;
182
 
183
    } // end of member function getTypeMime
184
 
185
    /**
186
     * Renvoie vrai si le document est un répertoire.
187
     *
188
     * @return bool
189
     * @access public
190
     */
191
    function isRepertoire( )
202 alexandre_ 192
    {	$isRep = is_dir ($this->_chemin) ;
193
        return $isRep ;
2 ddelon 194
    } // end of member function isRepertoire
195
 
196
 
197
    /**
198
     *
199
     *
200
     * @param int id_type_mime L'identifiant du type mime.
201
     * @return void
202
     * @access public
203
     */
204
    function setTypeMime( $id_type_mime )
205
    {
206
        $this->_type_mime = $id_type_mime ;
207
    } // end of member function set TypeMime
208
 
209
 
210
    /**
211
     * Renvoie la taille du fichier en octet. Nécessite un accès au disque.
212
     *
213
     * @return int
214
     * @access public
215
     */
216
    function getTaille( )
217
    {
218
        if ($this->isRepertoire()) {
219
            return $this->_tailleRepertoire($this->_chemin) ;
220
        }
202 alexandre_ 221
        return @filesize ($this->_chemin) ;
2 ddelon 222
    } // end of member function getTaille
223
 
224
    /**
225
     * Renovie le nom du fichier, sur le disque.
226
     *
227
     * @return string
228
     * @access public
229
     */
230
    function getNom( )
231
    {
232
 
233
    } // end of member function getNom
234
 
235
    /**
236
     * Renvoie le chemin du fichier.
237
     *
238
     * @return string
239
     * @access public
240
     */
241
    function getChemin( )
242
    {
243
        return $this->_chemin ;
244
    } // end of member function getChemin
245
 
246
    /**
247
     * Permet de calculer la taille en octet du repertoire courrant
248
     *
249
     * @return int
250
     * @access protected
251
     */
252
    function _tailleRepertoire($rep)
253
    {
254
        $taille = 0 ;
255
        $liste_fichier = scandir ($rep) ;
256
        foreach ($liste_fichier as $key => $value) {
257
            if (is_dir ($rep."/".$value) && $value != ".." && $value != ".") {
258
 
259
                $taille += $this->_tailleRepertoire ($rep.$value."/") ;
260
            } else {
202 alexandre_ 261
                if ($value != '..' && $value != '.') $taille += @filesize ($rep.$value) ;
2 ddelon 262
            }
263
        }
264
        return $taille ;
265
    } // end of member function _tailleRepertoire
266
 
267
 
268
    /**
269
     * initAttributes sets all fichier attributes to its default                    value make
270
     * sure to call this method within your class constructor
271
     */
272
    function initAttributes( )
273
    {
274
        $this->_type = 'fichier';
275
    }
276
 
277
 
278
} // end of fichier
279
 
280
if(!function_exists("scandir"))
281
{
282
  function scandir($dirstr)
283
  {
284
   // php.net/scandir (PHP5)
285
   $files = array();
286
   $fh = opendir($dirstr);
287
   while (false !== ($filename = readdir($fh)))
288
   {
289
     array_push($files, $filename);
290
   }
291
   closedir($fh);
292
   return $files;
293
  }
294
}
295
?>