0

Currently I have a code which is working perfectly for one image upload. But i want to upload multiple images at a time with same Image Title, Image Description for image group being uploaded as these images will be used in photo slideshow(See Images below please)

My Current Code is as follows -

PHP Code -

    $bsq->connect_db();
    $fileName = $_FILES["image"]["name"]; 
    $fileNameNew = preg_replace('/\s+/', '_', $fileName);
    $fileTmpLoc = $_FILES["image"]["tmp_name"];
    // Path and file name
    $pathAndName = "uploads_admin/".$fileNameNew;
    // Run the move_uploaded_file() function here
    $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
    // Evaluate the value returned from the function if needed


    if($_POST['action']=="add"){
           $all_columns[]="image_subject";
           $all_columns[]="image_name"; 
           $all_columns[]="clinic"; 
           $all_columns[]="image_link";



//Get All values to insert in to table      
           $all_values[]=addslashes($_POST["image_subject"]);
           $all_values[]=addslashes($_POST["image_name"]);  
           $all_values[]=addslashes($_POST["clinic"]);
           $all_values[]=addslashes($pathAndName );


//=====================

$qry=$bsq->webdreaminsert("sa_galleryuploads_by_admin",$all_columns,$all_values,'');
echo mysql_error();

header("location:upload_file_for_downloading_list.php");
    ///////////////////////////////////////////////////
    }   

And HTML Form For upload Image Is As follows -

      <form action="" method="post" enctype="multipart/form-data" name="addtwebinar1" id="addtwebinar1" onsubmit="javascript:return validateimage1();" >
      <input type="hidden" value="add" name="action" />

      <table width="90%" align="center" border="0" cellpadding="0" cellspacing="0" class="ListTable1">
      <tr class="HeadBr">
      <td colspan="4"><div align="center"><strong>Add Images For Photo Gallery</strong></div></td>
      </tr>

      <tr >
      <td>Image Title*</td>
      <td><input name="image_name" id="image_name" type="text" size="40" value="" /></td>
      </tr>
      <tr>   
      <td>Image Description In Short*</td>
      <td><input name="image_subject" id="image_subject" type="text" size="40" value="" /></td>
      </tr>
      <tr >
      <td>Clinic Name*</td>
      <td>
           <select name="clinic" id="message" >
        <option value="">Select Clinic</option>
        <option value="arogya">1. Arogyawardhini Ayurved Clinic</option>
        <option value="smruti">2. Smruti Ayurved Clinic</option>
        <option value="tarpan">3. Tarpan Ayurved Clinic</option>
        <option value="vishwa">4. Vishwawardhini Ayurved Clinic</option>
        </select>
              </td>
              </tr>                
              <tr >
                <td>Your Image For Upload* </td>
                <td><label for="image">File To Upload: </label><br>
                <input type="file" size="40" name="image"  id="image" /><br />
               </td>
               </tr>    

               <tr>
               <td></td>
               <td><button >Upload</button></td>
              </tr>
            </table>
            </form>

Current Look of My Active Image upload Form - enter image description here And I Want Like Below (Created Graphically) enter image description here

2
  • 2
    Your code is hideously dangerous. You're DIRECTLY using the user-provided ['name'] parameter to store the file on your server, allowing a malicious user to scribble their file ANYWHERE on your server. You're also probably vulnerable to SQL injection attacks, because addslashes() is utterly useless for preventing that. Commented Mar 19, 2014 at 14:10
  • thnx for suggestion. i am using this code to upload jpg, png and gif files only. validating file type before uploading. till is it dangerous? i am new in web designing. so suggest accordingly Commented Mar 19, 2014 at 16:11

1 Answer 1

1

You can use a for loop.

For the form, do something like this:

for($i = 1; $i <= 4; $i++) {
        echo "<input type=\"file\" size=\"40\" name=\"image{$i}\"  id=\"image{$i}\" /><br />";
}

And for the processing, just put all of that in a for loop as well.

   for($i = 1; $i <= 4; $i++) { 
   $fileName = $_FILES["image".$i]["name"]; 
   $fileNameNew = preg_replace('/\s+/', '_', $fileName); 
   $fileTmpLoc = $_FILES["image".$i]["tmp_name"]; 
    // Path and file name 
   $pathAndName = "uploads_admin/".$fileNameNew; 
   // Run the move_uploaded_file() function here 
   $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName); 
  // Evaluate the value returned from the function if needed 


  if($_POST['action']=="add"){ 

  $image_name = mysql_real_escape_string($_POST['image_name']); 
  $image_subject = mysql_real_escape_string($_POST['image_subject']); 
  $clinic = mysql_real_escape_string($_POST['clinic']); 
  $image_link = "http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), "\\/")."/".$pathAndName; 

  //===================== 

  mysql_query("INSERT INTO `sa_galleryuploads_by_admin` VALUES ('', '{$image_name}',  '{$image_subject}', '{$clinic}', '{$image_link}' )") or die(mysql_error()); 

  if(!mysql_error()) { 
     echo "success"; 
   } 

   } 

You can edit the number that the loop goes up to, to match the number of fields/images you want to show. Good luck!

edit: You also need to sanitize validate your inputs. At the very least, use mysql_real_escape_string().

Also, mysql_* functions are deprecated. You should switch to using either mysqli or pdo. I'd suggest mysqli to start off, because it also offers a procedural approach, whereas pdo is completely object oriented.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanx for code. But the Problem is, it is inserting link of first file chosen for upload...and uploading first two images in upload folder. what changes are required ?
@drManishJoshi You should store the image links in a different table. Can you show me your database insertion queries/corresponding php code?
@drManishJoshi or you can include your database insertion method/code to the for loop, and then just display the images on the actual page by using the WHERE name = 'IMAGE TITLE' or however your table is structured.
INSERT INTO table_name ( image_id, image_name, image_subject, clinic, image_link )
with above code , if 4 images are selected, 4 image_id should be auto incremented with same image_name, image_title, clinic and 4 different links 'upload_admin/1.jpg , upload_admin/2.jpg, upload_admin/3.jpg and upload_admin/4.jpg. am i right ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.