php - Insert form and multiple upload -


i want insert advertisement. advertisement contains title, text, price , can have 1 or multiple images. form on 1 page. php , html in 1 file.

in database have advertisement table , image table. both have unique id primary.

the image table contains filename , advertisement_id advertisement table foreign key table.

questions:

  1. how upload multiples files?

  2. do need insert everthing @ once or insert advertisement first , pictures?

<div class="form-group">     <label for="image"  class="col-sm-2 control-label">image:</label>     <div class="col-sm-6">         <input type="file" id="exampleinputfile" name="filename[]" id="filename" multiple">     </div> </div> 

as second question, thing more sensible insert advertisement insert pictures.

for first question,

the html code that

    <form name="multiupload" action="uploadmany.php" method="post" enctype="multipart/form-data">     <input name="filestoupload[]" type="file" multiple /></td>     <td collapse=2><input name="savemultiimg" type="submit" value="submit"></td>     </form> 

and here php code. variable $err notification of uploading state.

    <?php require("databaseconnection.php"); if(isset($_post["savemultiimg"])) {     for($i=0;$i<count($_files['filestoupload']['tmp_name']);$i++){         $name=$_files['filestoupload']['name'][$i];         $tmp_name=$_files['filestoupload']['tmp_name'][$i];   $target_dir = "images/";  //the dir want save images $target_file = $target_dir . rand().'_'.($name); //this name of img, , give random number if uploaded 2 image same name don't have problem $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image     $check = getimagesize($tmp_name);     if($check !== false) {         //echo "file image - " . $check["mime"] . ".";         $uploadok = 1;     } else {         $err= "file uploaded not image";         $uploadok = 0;     }//end if  // check if file exists,, appear if removed random number on top if (file_exists($target_file)) {     $err ="file exists";     $uploadok = 0; }//end if  if ($uploadok != 0) { if (move_uploaded_file($tmp_name, $target_file)) {     $err ="file uploaded succ.";     $sql = "...";//your sql here     $result = $con->query($sql);  } else {     $err ="unecpected error occured"; }   //end if upload      }//end ($uploadok != 0) }//end forloop header('location: index.php?err='.$err.'); } ?> 

this code 1 of university projects, , have modified suet you. if caused error tell me , i'll happy you.

the origin code copied w3schools ,and changes made multiple upload.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -