Solved

uploading multiple images

Posted on 2007-03-29
5
226 Views
Last Modified: 2013-12-12
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
Comment
Question by:Ameerh24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 150 total points
ID: 18816509
0
 
LVL 4

Accepted Solution

by:
secondv earned 350 total points
ID: 18817405
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
 

Author Comment

by:Ameerh24
ID: 18817700
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
 

Author Comment

by:Ameerh24
ID: 18819156
I found the error ..
$_FILES['userfile'][$i]['type'];    should be     $_FILES['userfile']['type'][$i];

all in all, thank u very much
0
 
LVL 4

Expert Comment

by:secondv
ID: 18819229
ahhh, I should have spotted that. Glad it helped you :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question