php - Image upload with zend framework -
i new zend framework. need create user registration image upload. have done that. registration controller , upload controller works. images uploading uploads/user-images/ in public folder. actual requirement is, have create specific folder each user based on user id , need upload photos folder. also, need store image path in db actualname_current_timestamp_width_height.extension, don't know how this. here including controller codes:
<?php /** * photocontroller class file * * file contains class photocontroller * * @category zend * @package zend_controller * */ class photocontroller extends daddara_controller_basecontroller { public $session; function init() { parent::init(); $this->view->headscript()->appendfile('/resources/js/upload.js'); $this->view->headscript()->appendfile('/resources/js/jquery.min.js'); $this->view->headscript()->appendfile('/resources/js/jquery.wallform.js'); $this->view->headscript()->appendfile('/resources/js/modernizr.custom.js'); // $this->view->headscript()->appendfile('/resources/js/lightbox/jquery-1.10.2.min.js'); // $this->view->headscript()->appendfile('/resources/js/lightbox/lightbox-2.6.min.js'); } function indexaction() { $this->_forward('upload'); } /** * upload action action * */ public function uploadaction() { $path = "uploads/user-images/"; $shortpath = "uploads/user-images/resize/"; function getextension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $em = $this->getentitymanager(); //user id $userid = $em->getrepository("models\user")->findoneby(array('id' => $this->currentuser->id)); //registration id $registrationid = $em->getrepository("models\registration")->findoneby(array('user' => $this->currentuser->id)); $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","png","jpg","jpeg","gif","bmp"); if($this->getrequest()->ispost()){ { $name = $_files['photoimg']['name']; $size = $_files['photoimg']['size']; if(strlen($name)) { $ext = getextension($name); //echo $ext;exit; if(in_array($ext,$valid_formats)) { if($size<(1024*1024)) { $actual_image_name = time().substr(str_replace(" ", "_", $ext), 5).".".$ext; $tmp = $_files['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { if($ext=="jpg" || $ext=="jpeg" ) { $uploadedfile = $path.$actual_image_name; $src = imagecreatefromjpeg($uploadedfile); } else if($ext=="png") { $uploadedfile = $path.$actual_image_name; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } list($width,$height)=getimagesize($uploadedfile); $newwidth=160; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); $filename = "uploads/user-images/resize/". $actual_image_name; imagejpeg($tmp,$filename,100); $photos = new models\profilephotos(); $photos->setuser($userid) ->setregistration($registrationid) ->setphoto($path.$actual_image_name) ->setshortimage($shortpath.$actual_image_name) ->setaddedon(); $em->persist($photos); $em->flush(); echo "<img src='/uploads/user-images/resize/".$actual_image_name."' class='preview'>"; } else echo "fail upload folder read access."; } else echo "image file size max 1 mb"; } else echo "invalid file format.."; } else echo "please select image..!"; exit; } } } public function listaction() { $em = $this->getentitymanager(); $photo = $em->getrepository('models\profilephotos')->findby(array('user' => $this->currentuser->id)); //print_r($image);exit; $this->view->photo = $photo; } /** * delete function */ function deleteaction() { $deleteid = $this->getrequest()->getparam('id'); if(empty($deleteid)){ throw new zend_controller_exception("page not found", 404); } $em = $this->getentitymanager(); $del = $em->find("models\profilephotos", $deleteid); $em->remove($del); $em->flush(); $this->addmessage('data deleted "'); $this->_redirect('/photo/list'); } }
you can create folder command mkdir():
if(mkdir(application_path . '/public/uploads/' . $userid)){ //dir created } you can move file copy() , unlink():
if(copy(application_path . '/public/uploads/' .$filename, application_path . '/public/uploads/' .$userid .'/' .$newfilename)) { unlink(application_path . '/public/uploads/' . $filename); }
Comments
Post a Comment