PDA

View Full Version : گفتگو: نظر دوستان در مورد کلاس اپلود چند عکس



فرزند کوروش
شنبه 31 فروردین 1392, 10:11 صبح
class image_upload{

public $filename;
public $formatexist;
public $sizeexist;
public $error = array();
public $path;
public $imageout;
public $url = array();
public $imageName = array();
function get_filename($value){ //esme forme file && name file field form
$this->filename = $value;

return $this->filename;
}
function getimageName(){
$this->imageName = $_FILES[$this->filename]['name']; //esme ax && picture name
return $this->imageName;
}
function get_formatexist($value){ //format haye mojaz baraye upload && valid format for upload
$this->formatexist = $value;
return $this->formatexist;
}
function get_sizeexist($value){ //size haye mojaz baraye upload && valid size for upload
$this->sizeexist = $value;
return $this->sizeexist;
}
function savepath($value){ // set folder save
$this->path = trim($value);
if(is_dir( $this->path)){
return $this->path;
}else{
$this->error[] = 'فولدر وجود ندارد';
}

}

function render(){
$name = '0';
@$fileType2 = $_FILES[$this->filename]['type'];
@$ortmp_name = $_FILES[$this->filename]['tmp_name'];
@$orsize = $_FILES[$this->filename]['size'];
@$orname = $_FILES[$this->filename]['name'];

$fileType = str_replace("image/","",$fileType2); //hazf e image/ az name file type

$this->formatexist = explode(',',$this->formatexist); //joda kardabe formathaye morede niyaz bar asas alamat , \
// echo $this->imageName;
foreach( $this->imageName as $idx => $name){

if (!in_array($fileType[$idx], $this->formatexist)){ //check kardan format e file
$this->error[] = "فرمت فایل نادرست است :".$orname[$idx];
}

$formatName = explode('.',$this->imageName[$idx]); //format e name form . jelogiri az hack header

if (!in_array($formatName[1], $this->formatexist)){ //check kardan format e file
$this->error[] = "پسوند فایل نادرست است :".$orname[$idx];
}


if( $this->sizeexist <= $orsize[$idx]){
$this->error[] = "حجم فایل نادرست است :".$orname[$idx];
}
}
if ( count($this->error) == '0'){
foreach( $this->imageName as $idx => $name){

$this->randomkey = md5(rand(100,10000));
//echo 'jjjjjj'.$this->path.'/'.$this->randomkey.$this->imageName[$idx];

if(is_uploaded_file($ortmp_name[$idx])){
if(copy($ortmp_name[$idx],$this->path.'/'.$this->randomkey.$this->imageName[$idx])){
$this->error[] = 'به خوبی ثبت شد';
$this->url[] = $this->path.$this->randomkey.$this->imageName[$idx];
};
};
}
}
}
function image_upload(){
return $this->error ;
}
function output_url(){
return $this->url;
}
function form_render($fname){
$form = "
<form method=\"post\" enctype=\"multipart/form-data\">
<input type=\"file\" name='".$fname."[]' multiple=\"multiple\" id=\"file\" />
<input type=\"submit\" id=\"send\" value=\"ارسال\" />
</form>
<div id=\"filelist\"></div>
";
return $form;
}
}



روش استفاده :

$img = new image_upload();
$img->get_filename('user');
$img->getimageName();
$img->get_formatexist('gif,png,jpeg,jpg');
$img->get_sizeexist('100000');
//$img->savepath('D:\\saman profile\\webroot2\\www\\class_proje\\image\\');
$folder = $_GET['folder'];
$img->savepath('../image/'.$folder);
$img->render();
echo $img-> form_render('user');
//echo var_dump($img);
foreach ( $img->image_upload() as $msg){ //نمایش پیامها
//echo "<img src='http://127.0.0.1/image_upload/$msg[1]' /> <br />";
echo error($msg).'<br>';
}
foreach ( $img->output_url() as $url){ //نمایش ادرس های اپلود شده
echo ok($url).'<br>';
}

emanab
یک شنبه 01 اردیبهشت 1392, 12:25 عصر
سلام.تشکر از اینکه حاصل تلاش خودتو در دسترس دیگران قرار میدی تا دیگران ازش بهره ببرن.ممنون:بوس: