Solved

uploading multiple images

Posted on 2007-03-29
5
224 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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