Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Upload.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Upload.php,v 1.2 2003/08/18 15:46:42 eric Exp $
12  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 //--------------------------------#
19 // LICENSE
20 //--------------------------------#
21 //
22 ///// fileupload.class /////
23 //Copyright (c) 1999 David Fox, Angryrobot Productions
24 //(http://www.angryrobot.com) All rights reserved.
25 //
26 //Redistribution and use in source and binary forms, with or without
27 //modification, are permitted provided that the following conditions are met:
28 //1. Redistributions of source code must retain the above copyright notice,
29 //this list of conditions and the following disclaimer.
30 //2. Redistributions in binary form must reproduce the above copyright notice,
31 //this list of conditions and the following disclaimer in the documentation
32 //and/or other materials provided with the distribution.
33 //3. Neither the name of author nor the names of its contributors may be used
34 //to endorse or promote products derived from this software without specific
35 //prior written permission.
36 //
37 //DISCLAIMER:
38 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
39 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 //DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
42 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
47 //THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 // ---------------------------------------------------------------------------
49 // $Id: Class.Upload.php,v 1.2 2003/08/18 15:46:42 eric Exp $
50 // $Log: Class.Upload.php,v $
51 // Revision 1.2 2003/08/18 15:46:42 eric
52 // phpdoc
53 //
54 // Revision 1.1 2002/01/08 12:41:34 eric
55 // first
56 //
57 // Revision 1.2 2001/02/08 12:07:18 marianne
58 // Creation de la directory 'path' si elle n'existe pas
59 //
60 // Revision 1.1 2001/01/22 11:53:04 marianne
61 // *** empty log message ***
62 //
63 // ---------------------------------------------------------------------------
64 //
66 /*
67 Error codes:
68  0 - "No file was uploaded"
69  1 - "Maximum file size exceeded"
70  2 - "Maximum image size exceeded"
71  3 - "Only specified file type may be uploaded"
72  4 - "File already exists" (save only)
73 */
74 
75 class uploader
76 {
77 
78  var $file;
79  var $errors;
80  var $accepted = "";
81  var $new_file = "";
82  var $max_filesize = 100000;
83  var $max_image_width = 1000;
84  var $max_image_height = 1000;
85 
86  function max_filesize($size)
87  {
88  $this->max_filesize = $size;
89  }
90 
91  function max_image_size($width, $height)
92  {
93  $this->max_image_width = $width;
94  $this->max_image_height = $height;
95  }
96 
97  function upload($filename, $accept_type, $extension)
98  {
99  // get all the properties of the file
100  $index = array(
101  "file",
102  "name",
103  "size",
104  "type"
105  );
106  for ($i = 0; $i < 4; $i++) {
107  $file_var = '$' . $filename . (($index[$i] != "file") ? "_" . $index[$i] : "");
108  eval('global ' . $file_var . ';');
109  eval('$this->file[$index[$i]]=' . $file_var . ';');
110  }
111 
112  if ($this->file["file"] && $this->file["file"] != "none") {
113  //test max size
114  if ($this->max_filesize && $this->file["size"] > $this->max_filesize) {
115  $this->errors[] = "Taille maximale dépassée:. Le fichier ne doit pas excéder " . $this->max_filesize / 1000 . "KB.";
116  return False;
117  }
118  if (preg_match("/image/", $this->file["type"])) {
119 
120  $image = getimagesize($this->file["file"]);
121  $this->file["width"] = $image[0];
122  $this->file["height"] = $image[1];
123  // test max image size
124  if (($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
125  $this->errors[] = "Les dimensions de l'image sont trop importantes. " . "L'image ne doit pas faire plus de : " . $this->max_image_width . " x " . $this->max_image_height . " pixels";
126  return False;
127  }
128  switch ($image[2]) {
129  case 1:
130  $this->file["extension"] = ".gif";
131  break;
132 
133  case 2:
134  $this->file["extension"] = ".jpg";
135  break;
136 
137  case 3:
138  $this->file["extension"] = ".png";
139  break;
140 
141  default:
142  $this->file["extension"] = $extension;
143  break;
144  }
145  } else if (!preg_match("/(\.)([a-z0-9]{3,5})$/", $this->file["name"]) && !$extension) {
146  // add new mime types here
147  switch ($this->file["type"]) {
148  case "text/plain":
149  $this->file["extension"] = ".txt";
150  break;
151 
152  default:
153  break;
154  }
155  } else {
156  $this->file["extension"] = $extension;
157  }
158  // check to see if the file is of type specified
159  if ($accept_type) {
160  $pattern = preg_quote($accept_type);
161  if (preg_match("/$pattern/", $this->file["type"])) {
162  $this->accepted = True;
163  } else {
164  $this->errors[] = "Seuls les fichiers de type " . preg_replace("/\|/", " or ", $accept_type) . " sont acceptés";
165  }
166  } else {
167  $this->accepted = True;
168  }
169  } else {
170  $this->errors[] = "Fichier introuvable...";
171  }
172  return $this->accepted;
173  }
174 
175  function save_file($path, $mode)
176  {
177  global $NEW_NAME;
178 
179  if ($this->accepted) {
180  if (!file_exists($path)) {
181  mkdir($path, 0775);
182  }
183  // very strict naming of file.. only lowercase letters,
184  // numbers and underscores
185  $new_name = preg_replace("/[^a-z0-9._]/", "", preg_replace("/ /", "_", preg_replace("/%20/", "_", strtolower($this->file["name"]))));
186  // check for extension and remove
187  if (preg_match("/(\.)([a-z0-9]{3,5})$/", $new_name)) {
188  $pos = strrpos($new_name, ".");
189  if (!isset($this->file["extension"])) {
190  $this->file["extension"] = substr($new_name, $pos, strlen($new_name));
191  }
192  $new_name = substr($new_name, 0, $pos);
193  }
194  $new_name = uniqid("") . "_" . $new_name;
195  if (!isset($this->file["extension"])) $this->file["extension"] = "";
196  $this->new_file = $path . $new_name . $this->file["extension"];
197  $NEW_NAME = $new_name . $this->file["extension"];
198 
199  switch ($mode) {
200  case 1: // overwrite mode
201  $aok = copy($this->file["file"], $this->new_file);
202  break;
203 
204  case 2: // create new with incremental extension
205  while (file_exists($path . $new_name . $copy . $this->file["extension"])) {
206  $copy = "_copy" . $n;
207  $n++;
208  }
209  $this->new_file = $path . $new_name . $copy . $this->file["extension"];
210  $aok = copy($this->file["file"], $this->new_file);
211  break;
212 
213  case 3: // do nothing if exists, highest protection
214  if (file_exists($this->new_file)) {
215  $this->errors[] = "Le fichier &quot" . $this->new_file . "&quot existe déjà";
216  } else {
217  $aok = rename($this->file["file"], $this->new_file);
218  }
219  break;
220 
221  default:
222  break;
223  }
224  if (!$aok) {
225  unset($this->new_file);
226  }
227  return $aok;
228  }
229  }
230 }
231 ?>
← centre documentaire © anakeen - published under CC License - Dynacase