• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

uploading multiple images

Hi,

I have written a php code to upload one or two or … or ten images (multple images) ..
But no any images is uploaded !!

The code is  :

require($_SERVER["DOCUMENT_ROOT"]."\config\db_config.php");
    $link = mysql_connect($db_host, $db_user, $db_password);
    //echo "sccuss";
    mysql_select_db($db_name,$link);


for ($i = 1; $i <= 10; $i+= 1) {
   $userfile = 'userfile'.$i;
echo "$userfile"; echo "<br>";
if (($HTTP_POST_FILES[$userfile][type] == "image/gif") || ($HTTP_POST_FILES[$userfile][type] == "image/pjpeg") || ($HTTP_POST_FILES[$userfile][type] == "image/x-png") || ($HTTP_POST_FILES[$userfile][type] == "image/x-png"))
{
echo "{$HTTP_POST_FILES[$userfile][type]}";
         $form_data = $HTTP_POST_FILES["$userfile"]["tmp_name"];
         echo "$form_data" ;
//move it to some temp dir
         $base_name = "C:/Program Files/Apache Group/tmp/".basename($form_data);
         move_uploaded_file($form_data, $base_name);
// read the file
         $data = addslashes(fread(fopen($base_name, "r"), filesize($base_name)));
//insert query
$imageName = '1-'.basename($_FILES["$userfile"]["name"]);
echo "$imageName";
        $query="insert INTO img_pg
                  VALUES ('$imageName','$data','{$HTTP_POST_FILES[userfile][size]}','{$HTTP_POST_FILES[userfile][type]}')";
                  mysql_query($query, $link) or die (mysql_error());
echo "done";
}
}


And the HTML code for the form is :

<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv=pragma content=no-cache>
<meta http-equiv=cache-control content=no-cache>
<meta http-equiv=expires content=0>
<meta content="" name=Keywords>
<meta content="" name=Description>
<TITLE>Upload Project</TITLE>

</HEAD>


<BODY >
<div id="toprightdiv" style="margin-left:250px; width: 602px; height: 522px">
<hr color = #800000      >
 <form enctype="multipart/form-data" action="add_proj.php" method="POST">

    <!-- MAX_FILE_SIZE must precede the file input field -->


    <!-- Name of input element determines name in $_FILES array -->
    <p>Name of project : <input name = "Proj_name" type = text></p>
   

      <fieldset style="padding: 2; width:608px; height:124px">
      <legend>Image 1</legend>
      
      <p>Send this file: <input name="userfile1" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name1" type = text  size="60"></p>
      </fieldset>
      
      <fieldset style="padding: 2; width:608px; height:124px">
      <legend>Image 2</legend>
      
      <p>Send this file: <input name="userfile2" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name2" type = text  size="60"></p>
      </fieldset>
      
      <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 3</legend>
      
      <p>Send this file: <input name="userfile3" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name3" type = text  size="60"></p>
      </fieldset>


      <fieldset style="padding: 2; width:608px; height:130px">
      <legend>Image 4</legend>
      
      <p>Send this file: <input name="userfile4" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name4" type = text  size="60"></p>
      </fieldset>
      
      <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 5</legend>
      
      <p>Send this file: <input name="userfile5" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name5" type = text  size="60"></p>
      </fieldset>
      
            <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 6</legend>
      
      <p>Send this file: <input name="userfile6" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name6" type = text  size="60"></p>
      </fieldset>
      
            <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 7</legend>
      
      <p>Send this file: <input name="userfile7" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name7" type = text  size="60"></p>
      </fieldset>
      
      
            <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 8</legend>
      
      <p>Send this file: <input name="userfile8" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name8" type = text  size="60"></p>
      </fieldset>
      
      
      <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 9</legend>
      
      <p>Send this file: <input name="userfile9" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name9" type = text  size="60"></p>
      </fieldset>

      <fieldset style="padding: 2; width:608px; height:118">
      <legend>Image 10</legend>
      
      <p>Send this file: <input name="userfile10" type="file" size="56" /></p>
      <p>The comment of this image is : <input name = "Proj_name10" type = text  size="60"></p>
      </fieldset>
      <hr color = #800000      >
      <br><br>&nbsp;<input type="submit" value="Upload Project" /></form>


</div>
</div>
</BODY>
</HTML>


So any help on it please?
0
Ameerh24
Asked:
Ameerh24
  • 2
  • 2
2 Solutions
 
Aamir SaeedCommented:
0
 
secondvCommented:
Your code, but cleaned up and modified, give it a try:

<?php

require($_SERVER['DOCUMENT_ROOT'] . '\config\db_config.php');
$link = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name,$link);

$types = array(
      'image/gif',
      'image/pjpeg',
      'image/x-png'      
);
$errors = array();

if (isset($_POST['submit']))
{
      for ($i = 0; $i <= 10; $i++)
      {
            if (is_uploaded_file($_FILES['userfile'][$i]['tmp_name']))
            {
                  if (in_array($_FILES['userfile'][$i]['type'], $types))
                  {
                        $base_name = 'C:/Program Files/Apache Group/tmp/' . basename($_FILES['userfile'][$i]['tmp_name']);

                        if (move_uploaded_file($_FILES['userfile'][$i]['tmp_name'], $base_name))
                        {
                              $data = mysql_escape_string(fread(fopen($base_name, 'r'), filesize($base_name)));
                              //insert query
                              $imageName = '1-' . basename($_FILES['userfile'][$i]['name']);
                              $imageSize = $_FILES['userfile'][$i]['size'];
                              $imageType = $_FILES['userfile'][$i]['type'];
                              
                              mysql_query("
                                    INSERT INTO img_pg
                                    VALUES ('$imageName', '$data', '$imageSize','$imageType')
                              ", $link) or die (mysql_error());
                        }
                        else
                        {
                              array_push($errors, "File: #$i ({$_FILES['userfile'][$i]['name']}) could not be uploaded.");
                        }
                  }
                  else
                  {
                        array_push($errors, "File: #$i ({$_FILES['userfile'][$i]['name']}) has an invalid type ({$_FILES['userfile'][$i]['type']}).");
                  }
            }
            else
            {
                  array_push($errors, "File: #$i ({$_FILES['userfile'][$i]['name']}) could not be uploaded.");
            }
      }
      echo 'Done.' . (count($errors)) ? ' - but with errors:<br /><br />' . implode("<br />\n", $errors) : '';
}

?>

<html>
<head>
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta content="" name="keywords" />
<meta content="" name="description" />
<title>Upload Project</title>
</head>

<body>

<div id="toprightdiv" style="margin-left:250px; width: 602px; height: 522px">
      <hr style="color: #800000;" />
      <form enctype="multipart/form-data" action="add_proj.php" method="POST">
      <!-- MAX_FILE_SIZE must precede the file input field -->
      <!-- Name of input element determines name in $_FILES array -->
      <p>Name of project : <input name="Proj_name" type="text" /></p>
      <fieldset style="padding: 2; width:608px; height:124px">
            <legend>Image 1</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name1" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:124px">
            <legend>Image 2</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name2" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 3</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name3" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:130px">
            <legend>Image 4</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name4" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 5</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name5" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 6</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name6" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 7</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name7" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 8</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name8" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 9</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name9" type="text" size="60" /></p>
      </fieldset>
      <fieldset style="padding: 2; width:608px; height:118">
            <legend>Image 10</legend>
            <p>Send this file: <input name="userfile[]" type="file" size="56" /></p>
            <p>The comment of this image is : <input name="Proj_name10" type="text" size="60" /></p>
      </fieldset>
      <hr style="color: #800000;" /><br /><br />
      <input type="submit" name="submit" value="Upload Project" />
      </form>
</div>

</body>
</html>
0
 
Ameerh24Author Commented:
hi secondv,
thanks very much but there is still an errors .. (I have checked database if there is any new records but there is not) .. the result is an upload_images.php is  :
 
- but with errors:

File: #0 () could not be uploaded.
File: #1 () could not be uploaded.
File: #2 () could not be uploaded.
File: #3 () could not be uploaded.
File: #4 () could not be uploaded.
File: #5 () could not be uploaded.
File: #6 () could not be uploaded.
File: #7 () could not be uploaded.
File: #8 () could not be uploaded.
File: #9 () could not be uploaded.
File: #10 () could not be uploaded.

any help please ?
0
 
Ameerh24Author Commented:
I found the error ..
$_FILES['userfile'][$i]['type'];    should be     $_FILES['userfile']['type'][$i];

all in all, thank u very much
0
 
secondvCommented:
ahhh, I should have spotted that. Glad it helped you :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now