PDA

View Full Version : اپلود و تغییر سایز عکس در php



artablog
دوشنبه 06 بهمن 1393, 11:23 صبح
سلام
یه کلاس برا اپلود عکس درست کردم تو این اپلودر اودم کاری کردم که وقتی عکسی اپلود میشه اگه مثلا پوشه 2015 نباشه یه پوشه درست میکنه بعد مثلا تو ماه ژانویه هستیم اگه پوشه 01 وجود نداشت یه پوشه 01 درست میکنه بعد عکس رو اپلود میکنه حالا من میخوام ببینم این کارایی که کردم درست کار میکنه یعنی بعدا مشکلی چه از لحاظ امنیتی چه اشکال در کارکرد برنامه پیش میاره یا نه
دم اینکه میخوام از عکس اصلی مثلا دو عکس دیگه با اسم و سایز دیگه درست کنه مثلا اگه عکس اصلی شد 51246585.jpg دو عکس دیگه بشم 100-50-51246585.jpg و 120-60-51246585.jpg

این خود کلاس هست :


<?phpclass fileDir { private $fileInfo; private $fileLocation; private $error; private $direct;
function __construct($dir){ $this->direct = $dir; if(!is_dir($this->direct)){ die('Supplied directory is not valid: '.$this->direct); } }
function upload($theFile){ $this->fileInfo = $theFile; $ext = strtolower(substr(strrchr($this->fileInfo['name'],'.'),1)); $filename = rand(0000,9999).time().'.'.$ext; $this->fileLocation = $this->direct . $filename; if(move_uploaded_file($this->fileInfo['tmp_name'], $this->fileLocation)){ return $filename; } else { return 'File could not be uploaded'; $this->error = "Error: File could not be uploaded.\n"; $this->error .= 'Here is some more debugging info:'; $this->error .= print_r($_FILES); } }
function overwrite($theFile){ $this->fileInfo = $theFile; $this->fileLocation = $this->direct . $this->fileInfo['name']; if(file_exists($this->fileLocation)){ $this->delete($this->fileInfo['name']); } return $this->upload($this->fileInfo); }
function location(){ return $this->fileLocation; }
function fileName(){ return $this->fileInfo['name']; }
function delete($fileName){ $this->fileLocation = $this->direct.$fileName; if(is_file($this->fileLocation)){ @unlink($this->fileLocation); return 'Your file was successfully deleted'; } else { return 'No such file exists: '.$this->fileLocation; } }
function reportError(){ return $this->error; } }?>
و اینم test.php :


<?php
if (!empty($_FILES['thumbnail']['name'])) { if (!file_exists('../uploads/'.date('Y').'/'.date('m').'')) { mkdir('../uploads/'.date('Y').'/'.date('m').'', 0777, true);}$up = new fileDir('../uploads/'.date('Y').'/'.date('m').'/');$thumbnail = $up->upload($_FILES['thumbnail']);} else {$thumbnail = '';}
?>

<form method="POST" action="" enctype="multipart/form-data"></tr> <tr> <td class="label">تصویر شاخص</td> <td class="forminput"> <input dir="ltr" type="file" name="thumbnail" size="60">

</tr>
<input type="submit" name="submit" value="ارسال" class="button medium blue" /></td>