PHP
·
发表于 6年以前
·
阅读量:8458
ÓÐËoÂÔͼ¹¦ÄÜ µ«ÊÇ ¸Ð¾o²»È«Ãae£¬¶øÇÒÓеaÎÊÌa£¬¼ÌÐøѧϰ£¬½«À´ÒÔºoÐÞ¸ÄÏÂ
<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file" name="file" /><input type="submit" name='submit' value="Ìa½»" ></form>
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/6/28
* Time: 21:04
*/
class upload{
protected $fileMine;//ÎļþÉÏ´«ÀaÐÍ
protected $filepath;//ÎļþÉÏ´«Â*¾¶
protected $filemax;//ÎļþÉÏ´«´oС
protected $fileExt;//ÎļþÉÏ´«¸ñʽ
protected $filename;//ÎļþÃu
protected $fileerror;//Îļþ³o´iÉeÖÃ
protected $fileflag;//Îļþ¼i²a
protected $fileinfo; //FILES
protected $ext; //ÎļþÀ(C)Õ¹
protected $path;
//ÎļþÉÏ´«
public function __construct($filename="file",$filemax=20000000,$filepath="./Uploads",$fileflag=true,$fileExt=array('jpg','exe'),$fileMine=array('image/jpeg'))
{
$this->filename=$filename;
$this->fileinfo=$_FILES[$this->filename];
$this->filemax=$filemax;
$this->filepath=$filepath;
$this->fileflag=$fileflag;
$this->fileExt=$fileExt;
$this->fileMine=$fileMine;
//var_dump($this->filename);
}
//´iÎoÅжÏ
public function UpError(){
if($this->fileinfo['error']>0){
switch($this->fileinfo['error'])
{
case 1:
$this->fileerror="ÉÏ´«Îļþ´oС³¬¹ý*þÎñÆ÷ÔÊÐiÉÏ´«µÄ×i´oÖµ£¬php.iniÖÐÉeÖÃupload_max_filesizeÑ¡ÏiÏÞÖƵÄÖµ ";
break;
case 2:
$this->fileerror="ÉÏ´«Îļþ´oС³¬¹ýHTML±iµ¥ÖÐÒþ²ØÓoMAX_FILE_SIZEÑ¡ÏiÖ¸¶¨µÄÖµ";
break;
case 3:
$this->fileerror="Îļþ²¿*Ö±»ÉÏ´«";
break;
case 4:
$this->fileerror="ûÓÐÑ¡ÔñÉÏ´«Îļþ";
break;
case 5:
$this->fileerror="δÕÒµ½ÁÙʱĿ¼";
break;
case 6:
$this->fileerror="ÎļþдÈeʧ°Ü";
break;
case 7:
$this->fileerror="phpÎļþÉÏ´«À(C)չûÓдo¿ª ";
break;
case 8:
$this->fileerror="";
break;
}
return false;
}
return true;
}
//¼i²aÎļþÀaÐÍ
public function UpMine(){
if(!in_array($this->fileinfo['type'],$this->fileMine)) {
$this->error="ÎļþÉÏ´«ÀaÐͲ»¶Ô";
return false;
}
return true;
}
//¼i²aÎļþ¸ñʽ
public function UpExt(){
$this->ext=pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION);
//var_dump($ext);
if(!in_array($this->ext,$this->fileExt)){
$this->fileerror="Îļþ¸ñʽ²»¶Ô";
return false;
}
return true;
}
//¼i²aÎļþÂ*¾¶
public function UpPath(){
if(!file_exists($this->filepath)){
mkdir($this->filepath,0777,true);
}
}
//¼i²aÎļþ´oС
public function UpSize(){
$max=$this->fileinfo['size'];
if($max>$this->filemax){
$this->fileerror="Îļþ¹ý´o";
return false;
}
return true;
}
//¼i²aÎļþÊÇ*ñHTTP
public function UpPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->fileerror="¶ñÒaÉϳ¥»¹";
return false;
}
return true;
}
//ÎļþÃu*ÀÖ¹Öظ´
public function Upname(){
return md5(uniqid(microtime(true),true));
}
//ͼƬËoÂÔͼ
public function Smallimg($x=100,$y=100){
$imgAtt=getimagesize($this->path);
//ͼÏñ¿i£¬¸ß£¬ÀaÐÍ
$imgWidth=$imgAtt[0];
$imgHeight=$imgAtt[1];
$imgext=$imgAtt[2];
//µÈ±ÈÁÐËo*Å
if(($x/$imgWidth)>($y/$imgHeight)){
$bl=$y/$imgHeight;
}else{
$bl=$x/$imgWidth;
}
$x=floor($imgWidth*$bl); //Ëo*źo
$y=floor($imgHeight*$bl);
$images=imagecreatetruecolor($x,$y);
$big=imagecreatefromjpeg($this->path);
imagecopyresized($images,$big,0,0,0,0,$x,$y,$imgWidth,$imgWidth);
switch($imgext){
case 1:
$imageout=imagecreatefromgif($this->path);
break;
case 2:
$imageout=imagecreatefromjpeg($this->path);
break;
case 3:
$imageout=imagecreatefromgif($this->path);
break;
}
$im=imagejpeg($images,$this->path);
}
//ÎļþË«´«
public function uploads()
{
if($this->UpError()&&$this->UpMine()&&$this->UpExt()&&$this->UpSize()&&$this->UpPost()){
$this->UpPath();
$names=$this->Upname();
$this->path=$this->filepath.'/'. $names.'.'.$this->ext;
if(move_uploaded_file($this->fileinfo['tmp_name'], $this->path)){
return $this->path;
}else{
$this->fileerror="ÉÏ´«Ê§°Ü";
}
}else{
exit("<b>".$this->fileerror."</b>");
}
}
}
<?php
header("content-type:imagejpeg");
header("Content-type:text/html;charset=utf-8");
require 'list.php';
$u=new upload();
$a=$u->uploads();
$c=$u->Smallimg();
echo "<img src={$a} />";
echo "<img src={$c} />";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<form action="ce.php" enctype="multipart/form-data" method="post" >
<input type="text" name="name" /><input type="file" name="file" />
<input type="submit" name='submit' value="Ìa½»" >
</form>
</body>
</html>
ÒÔÉÏËuÊoÊÇС±a¸ø´o¼Ò½eÉܵÄPHPÉÏ´«Í¼Æ¬ÀaÏÔʾËoÂÔͼ¹¦ÄܵÄÏa¹Ø֪ʶ£¬Ï£Íu¶Ô´o¼ÒÓÐËu°iÖu£¬Èç¹u´o¼ÒÓÐÈκÎÒÉÎÊÇe¸øÎÒÁoÑÔ£¬Ð¡±a»a¼°Ê±»Ø¸´´o¼ÒµÄ¡£ÔÚ´ËÒ²*dz£¸Ðл´o¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡