Need help using php to upload multiple images to different folders and store file names in mysql datatbase -


i have code uploads different image files different folders on server , stores file names in different fields in mysql database. have written codes , working fine. need how write codes such instead of using multiple codes blocks re-sizing , uploading each image, 1 code block able re-size multiple images still upload multiple images respective different folders , @ same time still store different file names in different fields in mysql database. appreciated.

below php code:

<?php function getextension($str) {           $i = strrpos($str,".");          if (!$i) { return ""; }            $l = strlen($str) - $i;          $ext = substr($str,$i+1,$l);          return $ext;  }   define ("max_size","2048");   $errors=0;  {  $image =$_files["passport"]["name"];  $uploadedfile = $_files['passport']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['passport']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['passport']['tmp_name']);     // check if file uploaded if( ! isset($_files['passport']) || ! is_uploaded_file($_files['passport']['tmp_name']))         exit('no file uploaded or passport has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your passport image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['passport']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['passport']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['passport']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $passport= rand()."_".time().".".$pixext;  $target = "passport/". $passport;  imagejpeg($tmp,$target,100); }   } }  {  $image =$_files["birthcert"]["name"];  $uploadedfile = $_files['birthcert']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['birthcert']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['birthcert']['tmp_name']);     // check if file uploaded if( ! isset($_files['birthcert']) || ! is_uploaded_file($_files['birthcert']['tmp_name']))         exit('no file uploaded or birth certificate has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your birth certificate image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['birthcert']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['birthcert']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['birthcert']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $birthcert= rand()."_".time().".".$pixext;  $target = "birthcert/". $birthcert;  imagejpeg($tmp,$target,100);  }   } }  {  $image =$_files["lastacadreport"]["name"];  $uploadedfile = $_files['lastacadreport']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['lastacadreport']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['lastacadreport']['tmp_name']);     // check if file uploaded if( ! isset($_files['lastacadreport']) || ! is_uploaded_file($_files['lastacadreport']['tmp_name']))         exit('no file uploaded or last academic report has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your last academic report image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['lastacadreport']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['lastacadreport']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['lastacadreport']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $lastacadreport= rand()."_".time().".".$pixext;  $target = "lastacadreport/". $lastacadreport;  imagejpeg($tmp,$target,100);  }   } }  {  $image =$_files["testimonial"]["name"];  $uploadedfile = $_files['testimonial']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['testimonial']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['testimonial']['tmp_name']);     // check if file uploaded if( ! isset($_files['testimonial']) || ! is_uploaded_file($_files['testimonial']['tmp_name']))         exit('no file uploaded or testimonial has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your testimonial image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['testimonial']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['testimonial']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['testimonial']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $testimonial= rand()."_".time().".".$pixext;  $target = "testimonial/". $testimonial;  imagejpeg($tmp,$target,100);  }   } }  {  $image =$_files["lastresult"]["name"];  $uploadedfile = $_files['lastresult']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['lastresult']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['lastresult']['tmp_name']);     // check if file uploaded if( ! isset($_files['lastresult']) || ! is_uploaded_file($_files['lastresult']['tmp_name']))         exit('no file uploaded or last result has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your last result image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['lastresult']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['lastresult']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['lastresult']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $lastresult= rand()."_".time().".".$pixext;  $target = "lastresult/". $lastresult;  imagejpeg($tmp,$target,100);  }   } }  {  $image =$_files["transfercert"]["name"];  $uploadedfile = $_files['transfercert']['tmp_name'];    if ($image)    {   $filename = stripslashes($_files['transfercert']['name']);   $extension = getextension($filename);   $extension = strtolower($extension);    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))    {     echo ' unknown image extension ';     $errors=1;   }  else   {    $size=filesize($_files['transfercert']['tmp_name']);     // check if file uploaded if( ! isset($_files['transfercert']) || ! is_uploaded_file($_files['transfercert']['tmp_name']))         exit('no file uploaded or transfer certificate has exceeded size limit of 2mb. click botton on browser re-enter right size of image.');    if ($size > max_size*1024)   {     echo "your transfer certificate image has exceeded size limit";     $errors=1;   }      if($extension=="jpg" || $extension=="jpeg" )     {     $uploadedfile = $_files['transfercert']['tmp_name'];     $src = imagecreatefromjpeg($uploadedfile);     }     else if($extension=="png")     {     $uploadedfile = $_files['transfercert']['tmp_name'];     $src = imagecreatefrompng($uploadedfile);     }     else      {     $src = imagecreatefromgif($uploadedfile);     }  list($width,$height)=getimagesize($uploadedfile);  $newwidth=1120; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight);  imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);  //$set['passport'] = "'" . $pix . "'"; $pix= $_files['transfercert']['name']; $kaboom = explode(".", $pix); $pixext = end($kaboom); $transfercert= rand()."_".time().".".$pixext;  $target = "transfercert/". $transfercert;  imagejpeg($tmp,$target,100);  }   } imagedestroy($src); imagedestroy($tmp); }  $editformaction = $_server['php_self']; if (isset($_server['query_string'])) {   $editformaction .= "?" . htmlentities($_server['query_string']); }  if ((isset($_post["mm_insert"])) && ($_post["mm_insert"] == "onlineapp")) {   $insertsql = sprintf("insert wp1_onlineform (surname, othernames, sex, dateofbirth, placeofbirth, stateoforigin, nameofparent, occupationofparent, officeadd, officephone, homephone, homeaddress, previousschool, addofprevschl, highestclass, admissionclass, passport, birthcert, acadreport, testimonial, lastresult, transfercert) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, '$passport', '$birthcert', '$lastacadreport', '$testimonial', '$lastresult', '$transfercert')",                        getsqlvaluestring($_post['surname'], "text"),                        getsqlvaluestring($_post['othernames'], "text"),                        getsqlvaluestring($_post['sex'], "text"),                        getsqlvaluestring($_post['dob'], "text"),                        getsqlvaluestring($_post['placeofbirth'], "text"),                        getsqlvaluestring($_post['state'], "text"),                        getsqlvaluestring($_post['parentname'], "text"),                        getsqlvaluestring($_post['occupation'], "text"),                        getsqlvaluestring($_post['officeaddress'], "text"),                        getsqlvaluestring($_post['officephone'], "text"),                        getsqlvaluestring($_post['homephone'], "text"),                        getsqlvaluestring($_post['homeaddress'], "text"),                        getsqlvaluestring($_post['prevschoolname'], "text"),                        getsqlvaluestring($_post['prevschooladd'], "text"),                        getsqlvaluestring($_post['highestclass'], "text"),                        getsqlvaluestring($_post['admissionclass'], "text"));    mysql_select_db($database_connpacific, $connpacific);   $result1 = mysql_query($insertsql, $connpacific) or die(mysql_error());    $insertgoto = "confirm.php";   if (isset($_server['query_string'])) {     $insertgoto .= (strpos($insertgoto, '?')) ? "&" : "?";     $insertgoto .= $_server['query_string'];   }   header(sprintf("location: %s", $insertgoto)); } ?> 

this code form:

<form action="<?php echo $editformaction; ?>" method="post" enctype="multipart/form-data" name="onlineapp">               <table width="760" border="0" cellspacing="0" cellpadding="10">                 <tr>                   <td width="276" id="maintext">surname:</td>                   <td width="444" id="maintext2"><label for="surname"></label>                   <input name="surname" type="text" class="textfield" id="surname" size="45"></td>                 </tr>                 <tr>                   <td id="maintext">other names:</td>                   <td id="maintext2"><input name="othernames" type="text" class="textfield" id="othernames" size="45"></td>                 </tr>                 <tr>                   <td id="maintext">sex:</td>                   <td id="maintext2"><select name="sex" class="textfield" id="sex">                     <option selected>- select 1 -</option>                     <option value="male">male</option>                     <option value="female">female</option>                   </select></td>                 </tr>                 <tr>                   <td id="maintext">date of birth:</td>                   <td id="maintext2"><input name="dob" type="text" class="textfield" id="dob" size="20">                      (dd-mm-yyyy)</td>                 </tr>                 <tr>                   <td id="maintext">place of birth:</td>                   <td id="maintext2"><input name="placeofbirth" type="text" class="textfield" id="placeofbirth" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">state of origin:</td>                   <td id="maintext2"><input name="state" type="text" class="textfield" id="state" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">name of parent/guardian:</td>                   <td id="maintext2"><input name="parentname" type="text" class="textfield" id="parentname" size="45"></td>                 </tr>                 <tr>                   <td id="maintext">occupation of parent/guardian:</td>                   <td><input name="occupation" type="text" class="textfield" id="occupation" size="45"></td>                 </tr>                 <tr>                   <td id="maintext">office address  of parent/guardian:</td>                   <td id="maintext2"><input name="officeaddress" type="text" class="textfield" id="officeaddress" size="70"></td>                 </tr>                 <tr>                   <td id="maintext">office phone:</td>                   <td id="maintext2"><input name="officephone" type="text" class="textfield" id="officephone" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">home phone:</td>                   <td id="maintext2"><input name="homephone" type="text" class="textfield" id="homephone" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">home address</td>                   <td id="maintext2"><input name="homeaddress" type="text" class="textfield" id="homeaddress" size="70"></td>                 </tr>                 <tr>                   <td id="maintext">name of previous school:</td>                   <td id="maintext2"><input name="prevschoolname" type="text" class="textfield" id="prevschoolname" size="45"></td>                 </tr>                 <tr>                   <td id="maintext">address of previous school:</td>                   <td id="maintext2"><input name="prevschooladd" type="text" class="textfield" id="prevschooladd" size="70"></td>                 </tr>                 <tr>                   <td id="maintext">highest clsss passed @ previous school:</td>                   <td id="maintext2"><input name="highestclass" type="text" class="textfield" id="highestclass" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">class admission being sougth:</td>                   <td id="maintext2"><input name="admissionclass" type="text" class="textfield" id="admissionclass" size="30"></td>                 </tr>                 <tr>                   <td id="maintext">&nbsp;</td>                   <td id="maintext2"><label for="passport"><strong>note: uploads must below 2mb</strong></label></td>                 </tr>                 <tr>                   <td id="maintext">passport photo:</td>                   <td id="maintext2"><input type="file" name="passport" id="passport"></td>                 </tr>                 <tr>                   <td id="maintext">birth certificate:</td>                   <td id="maintext2"><input type="file" name="birthcert" id="birthcert"></td>                 </tr>                 <tr>                   <td id="maintext">last academic report child&rsquo;s current school:</td>                   <td id="maintext2"><input type="file" name="lastacadreport" id="lastacadreport"></td>                 </tr>                 <tr>                   <td id="maintext">character testimonial current school:</td>                   <td id="maintext2"><input type="file" name="testimonial" id="testimonial"></td>                 </tr>                 <tr>                   <td id="maintext">&nbsp;</td>                   <td id="maintext2"><strong>for transfer students only</strong></td>                 </tr>                 <tr>                   <td id="maintext">last result former school::</td>                   <td id="maintext2"><input type="file" name="lastresult" id="lastresult"></td>                 </tr>                 <tr>                   <td id="maintext">transfer certificate former school:</td>                   <td id="maintext2"><input type="file" name="transfercert" id="transfercert"></td>                 </tr>                 <tr>                   <td>&nbsp;</td>                   <td id="maintext2"><input name="button" type="submit" class="button" id="button" value="submit form"></td>                 </tr>               </table>               <input type="hidden" name="mm_insert" value="onlineapp">             </form> 

create function accept post file , after resizing , writing return array of file information. call every post file after file validation. if return array of information none error. database staff here example

function imagestaff($file){ // here resizing writing staff  // return array index error , file information if no error set index empty }  //call here post file if(valid($_files['your_file')){ // valid function of yours check if file size ok, or check uploaded file_type etc   $array = imagestaff($_files['your_file']);  if(empty($array['error'])){    //do db staff   }  } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -