 |
ปัญหาเรื่อง File Field ขอถามหน่อยนะครับ ช่วยทีงมมาทั้งวันทั้งคืน ไม่ได้ซักที |
|
 |
|
|
 |
 |
|
class UpImage อยู่ตรงไหนครับ
|
 |
 |
 |
 |
Date :
2012-10-15 23:06:27 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่คือ คำสั่ง สำหรับ up images ครับ โดยมันจะ include ไปยังไฟล์ class-up-img.php
Code
<?php include("class-up-img.php");
if(!empty($_POST["bt_up"])){
$file = new UpImage($_FILES["file"],"image/",1048576);
$re = $file->upImage();
if($re){
echo '<div style="text-align:left; padding: 5px; width:300px; margin:auto; ">Name: '.$file->getName().'<br/>';
echo 'Type: '.$file->getType().'<br/>';
echo 'Size: '.$file->getSize().'<br/></div>';
echo '<img src="image/'.$file->getName().'" width="200">';
}
else {
echo "error!";
}
}
?>
นี่คือโค้ดไฟล์ class-up-img.php
Code
<?php
class UpImage{
public $file;
private $name;
private $size;
private $type;
private $dri;
private $SISE = 1048576;
private $TYPE = array('png','jpg','gif');
private $NAME = "'/^[a-z\d]{4,30}$/i";
public function __construct($file,$dri,$zise){
//set value
$this->dri = $dri;
$this->file = $file;
$this->SISE = $zise;
$this->size = $file["size"];
// separate name and type
$Arr = explode(".",$file["name"]);
$this->name = $Arr[0];
$this->type = $Arr[1];
}
//Add type
public function addType($type){
array_push($this->TYPE,$type) ;
}
//Check accuracy of image file.
public function check(){
if($this->checkSize()&&$this->checkType()&&$this->checkError())
return true;
else
return false;
}
//Chack Size of image file.
public function checkSize(){
if($this->SISE < $this->size||0 > $this->size) return false;
else return true;
}
//Check Type of image file.
public function checkType(){
$b = false;
$count = count($this->TYPE);
while($count>0){
if($this->TYPE[$count-1]==$this->type){$b = true;
}
$count--;
}
return true;
}
//Check error of image file
public function checkError(){
if($file["error"]==0){
return true;
}
else{
return false;
}
}
//Chack name of image file
public function checkName(){
//will add "001" in end of name image file
if(!file_exists($this->dri.$this->name."00001.".$this->type)){
//set name of image
$this->NAME = $this->name."00001.".$this->type;
}
else{
$b=true; $i=1;
while($b){
$num = $this->name.(str_pad(($i++),5,"0",STR_PAD_LEFT));
if(!file_exists($this->dri.$num.".".$this->type)){
//set name of image
$this->NAME = $num.".".$this->type;
// out loop
$b=false;
}
}
}
}
//Upload image
public function upImage(){
if($this->check()){
$this->checkName();
$move = move_uploaded_file($this->file["tmp_name"],$this->dri.$this->NAME);
//Up image completed.
if($move)
$b = true;
//image is error!
else $b = false;
}
else {
// Image is error!
$b = false;
}
return $b;
}
public function getName(){
return $this->NAME;
}
public function getType(){
return $this->type;
}
public function getSize(){
return $this->size;
}
}
?>
|
 |
 |
 |
 |
Date :
2012-10-15 23:19:01 |
By :
ชัชวาลย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จริงๆ class name มันก็สื่อความหมายของมันอยู่นะครับ
private $TYPE = array('png','jpg','gif'); มันรับแค่ image
ถ้าจะอัปโหลดไฟล์ ก็ใช้ move_upload_file ธรรมดา ก็พอครับ
|
 |
 |
 |
 |
Date :
2012-10-15 23:54:04 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|