A good image upload class using php mysql -
i have written class handle image uploads in php. image upload class? if see code given below, insert mysql query happens when move_uploaded_file() function uploads file server. otherwise exits error. there better practise other this.? need other situations or exceptions or methods.?
<?php /** * image upload calss */ class images // define calss { public $_remane = false; //remane false public $_savetodb; public $_target_dir = "uploads"; function __construct()//constructor { # code... } public function displayimages($userids){ $qr = mysql_query("select id,img_name, img_order images usersid =".$userids." order img_order"); $resp = array(); while($row = mysql_fetch_array($qr)) { $resp[] = array("img_id" => $row['id'],"img_name" => $row['img_name'],"img_order" => $row['img_order']); } return $resp; }// end of displayimages public function displayallimages($userids = null){ if($userids == null){ $usersdata = mysql_query("select group_concat(id) uid users role_id =2"); $allids=mysql_fetch_object($usersdata); $userids = $allids->uid; } $qr = mysql_query("select img_name, usersid, users.email_id images left join users on images.usersid = users.id usersid in (".$userids.")"); $resp = array(); if($qr){ while($row = mysql_fetch_array($qr)) { $resp[] = array("email_id" => $row['email_id'],"img_name" => $row['img_name'],"usersid" => $row['usersid']); } return $resp; } else{ return false; } } public function validateimages(){ foreach ($_files['filetoupload']['name'] $f => $name) { //check each image type. $check = getimagesize($_files["filetoupload"]["tmp_name"][$f]); if($check == false) { $uploadok = 0; return false; } } return true; }//end of validateimages public function handelupload(){ //check if folder exixts if (!file_exists($this->_target_dir)) { mkdir($this->_target_dir, 0777, true); } $uploadok = 1; foreach ($_files['filetoupload']['name'] $f => $name) { $imagefiletype = pathinfo($_files["filetoupload"]["name"][$f],pathinfo_extension); if($this->_remane === true){ $imagefilename = substr(md5(rand()), 0, 7)."_".time().'.'.$imagefiletype; } else{ $imagefilename = basename($_files["filetoupload"]["name"][$f]); } $target_file = $this->_target_dir.'/'.$imagefilename; $count=0; // check if $uploadok set 0 error if ($uploadok == 0) { // if ok, try upload file } else { if (move_uploaded_file($_files["filetoupload"]["tmp_name"][$f], $target_file)) { try { $qr = mysql_query("select id images usersid = '".$_session['userid']."'"); $order = mysql_num_rows($qr); $qr = mysql_query("insert images(usersid, img_name, img_order) values('".$_session['userid']."','".$imagefilename."','".$order."')") or die(mysql_error()); continue; }catch (exception $e) { echo 'caught exception: ', $e->getmessage(), "\n"; }// end of catch } //end of if upload }//end of else }//end foreach if($uploadok == 1){ //image upload successful return true; }else{ //image upload fails return false; } }//end of handelupload }// end of calss ?> <form action="" method="post" enctype="multipart/form-data"> <fieldset> select image upload: <input type="file" name="filetoupload[]" id="filetoupload" multiple="multiple" accept="image/*"> </fieldset> <fieldset> <input type="submit" value="upload image" name="submit"> </fieldset> </form>
Comments
Post a Comment