Solved

uploading multiple images

Posted on 2007-03-29
5
223 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
Comment Utility
0
 
LVL 4

Accepted Solution

by:
secondv earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ahhh, I should have spotted that. Glad it helped you :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now