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