Contrôler la taille de l'image avant l'upload

WRInaute occasionnel
bonjour,
Voici le fichier d'upload que j'ai testé :

Code:
<?


class Upload {
    var $up_file;
	var $f;
    var $p;
	var $name;
	var $size;
	var $widht;
	var $height;
	var $type;
	var $max_size;
	var $path;
	var $directory;
	var $errors;
	var $move;
	
	function Upload($input_name,$size,$width,$height,$dir) {
	    $this->f = $input_name;
		$this->max_size = $size;
		$this->directory = $dir;
		$this->max_width = $width;
		$this->max_height = $height;
	}
	
  function do_upload() {
  //global $_FILES;
	    $this->up_file = $_FILES[$this->f]['tmp_name'];
		$this->name = $_FILES[$this->f]['name'];
		$this->size = $_FILES[$this->f]['size']/1000;
   	    $this->type = $_FILES[$this->f]['type'];
        if    (ereg(".gif$", $this->type)) $this->type="gif";
        elseif((ereg(".png$", $this->type)||
               ereg(".x-png$", $this->type))) $this->type="png";
        elseif((ereg(".jpg$", $this->type)||
               ereg(".jpeg$", $this->type)||
               ereg(".pjpeg$", $this->type))) $this->type="jpg";
        else $this->type="";
		$this->path = $this->directory;
        $this->width = $this->height = '';
        if(!empty($this->up_file)&&(C_HACK3)) {
        $this->p = getimagesize($this->up_file);
        $this->width = $this->p[0];  
        $this->height = $this->p[1];
        }  
		$this->errors = '';
		
	  if($this->move_file()) return true;
      else return false;
	}
	
	function getName() {
	  return $this->name;
	}
	
	function getMaxSize() {
	  return $this->max_size;
	}
	
	function getSize() {
	  return $this->size;
	}

	function getType() {
	  return $this->type;
	}
	
	function move_file() {
	  if($this->check_size() && $this->check_extension() && $this->check_uploaded() && 

$this->check_wh()) {
			if(@is_writable(dirname($this->path))) 
			{
				$this->move = move_uploaded_file($this->up_file, $this-

>path.$this->type);
      	@chmod($this->path.$this->type, 0644);    
				return true;
			} 
			else 
			{
				$this->errors = 'Directory ' . str_replace(C_PATH,'',dirname

($this->path)) . ' doesn`t writable.';
				return false;
			}	
		} else {
		  return false;
		}
	}
	
	function check_size() {
    global $w; 
	  if(($this->up_file != "")&&($this->size <= $this->max_size)) {
      return true;
		} else {
		  if($this->errors == '') {
              $tm=array($this->max_size,$this->size);
			  $this->errors = $this->errors.template($w[220],$tm);
			}
		  return false;
		}
	}
	
	function check_wh() { // Check width and height
    global $w; 
	  if(($this->up_file != "")&&($this->width <= $this->max_width)&&($this->height <= 

$this->max_height)) {
      return true;
		} else {
		  if($this->errors == '') {
              $tm=array($this->max_width,$this->max_height);
			  $this->errors = $this->errors.template($w[2201],$tm);
			}
		  return false;
		}
	}
	
	function check_uploaded() {
    global $w; 
	  if(is_uploaded_file($this->up_file)) {
			return true;
		} else {
		  if($this->errors == '') {
			  $this->errors = $this->errors.$w[1].'4';
			}
		  return false;
		}
	}
	
	function check_extension() {
    global $w; 
  		if ($this->type == "") {
  		    if($this->errors == '') {
  			   $this->errors = $this->errors.$w[222].$this->type;
  			}
  		  return false;
  		} else {
        return true;
		}
	}
	
	function getErrors() {
	  return $this->errors;
	}
	
}
?>

quand j'essai d'uploader un image, le fichier ne contrôle pas la taille de l'image (size).
J'ai fixé le size à 30 ko, et il upload les images < = 30 ko mais il ne donne pas d'alertes quand l'image est >=30 ko.

Merci pour votre aide
 
WRInaute occasionnel
Salut trustno1

$my_max_file_size = "30000"; //en octets
// controle poids :
if ($_FILES['the_file']['size'] > $my_max_file_size )
{


{echo "<font size=\"2\" color=\"#FF0000\">Votre image a une taille de ".sprintf("%01.3f", ($_FILES['the_file']['size']/1000000))." Mega Octets. <br>La taille maximum autorisée est de ".($my_max_file_size / 1000000)." Mega Octets.<br><br><a href=\"javascript:window.history.back()\">Retour</font></a>";exit;}
}

Voila, moi j'utilise ca pour controler le poids des fichiers uploadé

a+

Robin
 
WRInaute occasionnel
Bonjour comparef. Merci pour le code. mais je n'ai pas pu l'integré dans le fichier d'upload. j'ai mis le code du fichier entier en haut.
merci
 
Discussions similaires
Haut