How to upload Multiple files into the database

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2354 Views - Last Post: 18 September 2014 - 04:51 AM Rate Topic: -----

#1 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

How to upload Multiple files into the database

Posted 15 September 2014 - 10:49 AM


<form action = "uploadFile.php" method= "POST"  enctype = "multipart/form-data">
      <input type = "file" name = "file[]"><br><br>
      <input type = "file" name = "file[]"><br><br>
      <input type = "submit" value = "Submit">
</form>



Is it possible to upload more than one file at the same time with just a click on the submit button?
I am having this problem of figuring out what to do as to uploading multiple files at the same time? Any idea please?

Is This A Good Question/Topic? 0
  • +

Replies To: How to upload Multiple files into the database

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: How to upload Multiple files into the database

Posted 15 September 2014 - 11:03 AM

Does it have to be at the same time? Why not in sequence?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Re: How to upload Multiple files into the database

Posted 15 September 2014 - 11:06 AM

The files are already in the $_FILES array (not $_POST) when they arrive to the server. Not sure why you are asking about 'uploading to the database', because that's not at all what it's doing.
Was This Post Helpful? 0
  • +
  • -

#4 ge∅   User is offline

  • D.I.C Lover

Reputation: 193
  • View blog
  • Posts: 1,185
  • Joined: 21-November 13

Re: How to upload Multiple files into the database

Posted 16 September 2014 - 12:47 AM

I would use iframes. It's the easiest and most compatible way to do it, and it doesn't require a Java/Flash plugin.

You simply create as many iframes as you need file inputs. The files will be sent independently in their own frame without reloading the parent window. It's important to notify the user that he shouldn't leave the page until every file has finished uploading, and you will need to group the forms back together on the server side.
Was This Post Helpful? 0
  • +
  • -

#5 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 04:00 AM

@ ge0, how will one be using iframes? dont you think, that one will actually end up making the whole process complex?
Was This Post Helpful? 0
  • +
  • -

#6 ge∅   User is offline

  • D.I.C Lover

Reputation: 193
  • View blog
  • Posts: 1,185
  • Joined: 21-November 13

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 04:32 AM

I must have misunderstood the question. When you upload multiple files, since the upstream speed is slow, you usually want them to start uploading as soon as possible and independently of the rest of the form (think of Gmail attached files) because it makes the user experience smoother.

If you don't want that just pick no2pencil's answer. You only need to change your PHP script.

This post has been edited by ge∅: 17 September 2014 - 04:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 05:01 AM

I have used the following code to upload multiple files (images, in this case) to a folder location. If you search "php upload multiple files" or similar you will find many tutorials as it is a fairly common task.

In this code "inpFile" is what you have named "file".

Spoiler

Have a look through the Tutorials link at the top of this page as well, in case there is something relevant there.

This post has been edited by andrewsw: 17 September 2014 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 05:13 AM

Yeah! Thanks a great deal @ andrew?
But how will one be able to view an immediately updated file, to make things simple, I have been working my ways past how to display the image immediately its inserted into the database, but have I have been hitting the brick wall in this regard, because the image just won't display, but it's instead showing me a broken image.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 05:23 AM

To clarify, have you successfully managed to uploads the files/images and store them in your database?

If this is the case, and you are now struggling to retrieve and display the images (from the database), then you need to post the code that isn't working.
Was This Post Helpful? 0
  • +
  • -

#10 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 05:47 AM

My Respect to your ingenuity and willingness to be helpful, once again, RESPECT.
I have actually not even started working on displaying the multiple images sent into the database.
I asked myself "why don't you first display the image/file for one instance first, then you can now go ahead to display the image for multiple files", and that has been a big task for me. I have been having broken images that isn't displaying any thing.

Below are the two different script that i have used and alongside a description of my presently used database. And maybe when I have successfully been able to view an image at an instance, then i will possess the tricky and skill needed to view multiple images as they are being sent into the database.

.........................................................................
First is a description of my database: uploaded ("file_id", "name", "type", "size", "image{BLOB}", "email").

Next comes the Script: uploadFile.php: {I have imagined that I already have about 5 images as my id and so automatically an increment of 5 is in place, that's in respect of my file_id}
<?php 

//Open a new connection to the MySQL server
$user="root";
$host="localhost";
$password="";
$database = "dynamic_data";

$cxn = mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


?>



<?php

@$name = $_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
@$tmp_name = $_FILES['file']['tmp_name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
$max_size = 74752;




if(isset($name)){

  if(!empty($name)){
  	
    if(($extension == 'jpg' || $extension == 'jpeg')&& $type == 'image/jpeg' && $size <= $max_size){
		
	// Image submitted by form. Open it for reading (mode "r")
		$fp = fopen($_FILES['file']['tmp_name'], "r");
		
		// If successful, read from the file pointer using the size of the file (in bytes) as the length.
		if ($fp) {
			$content = fread($fp, $_FILES['file']['size']);
			fclose($fp);
		
			// Add slashes to the content so that it will escape special characters.
			// As pointed out, mysql_real_escape_string can be used here as well. Your choice.
			$content = addslashes($content);
			$content= mysqli_real_escape_string($cxn, $content);
			$name= mysqli_real_escape_string($cxn, $name);
			// Insert into the table "table" for column "image" with our binary string of data ("content")
			mysqli_query($cxn,"INSERT INTO uploaded (file_id, name, type, size, image, email) Values('','$name','$type', '$size','$content','[email protected]')") or 
			die("Couldn't execute query in your database!".mysqli_error($cxn));
			
			echo 'Data-File was inserted into the database!|';
			echo '<a href="showImages.php?id=5">View</a>';
		}
		
    else{
      echo 'There was an error!';
    }
  }
  else{
    echo 'File must be jpg/jpeg and must be 73 kilobyte or less! ';
  }

}

  else {
  echo 'Please select a file!';

  }
}

?>

<form action = "uploadFile.php" method= "POST"  enctype = "multipart/form-data">
      <input type = "file" name = "file"><br><br>
      <input type = "submit" value = "Submit">
</form>



Next, I have a script "showImages.php", that is being navigated to through the view link from script uploadFile.php after the successful insertion of an image.

<?php
//Open a new connection to the MySQL server
$user="root";
$host="localhost";
$password="";
$database = "dynamic_data";

$cxn = mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno()) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_GET['id'])) {
	$id= mysqli_real_escape_string($cxn, $_GET['id']);
	$mysql_run=mysqli_query($cxn, "SELECT * FROM uploaded WHERE file_id ='$id';");
	
	while ($row=mysqli_fetch_assoc($mysql_run)) {
		$image=$row['image'];
	}
	
	header("content-type: image/jpeg");
	echo $image;
}

else {
	echo 'Error!';
}





Your help will be highly appreciated. And maybe after I have been able to fully understand this concept, then one could move ahead to dealing with multiple file. Thanks you all!
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 05:55 AM

Quote

I have actually not even started working on displaying the multiple images sent into the database.

So what stage are you at? Have you managed to upload images into the database? Have you confirmed that they have been successfully inserted into the database?

Only once you have achieved this, and are sure that the images exist in the database, can you consider retrieving and displaying these images.
Was This Post Helpful? 0
  • +
  • -

#12 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 06:11 AM

The codes that I have in the prequel to this present post successfully inserts the files into my database,but the next step is in viewing the image at the instance of the insert, which is what the second script "showImages.php" in collaboration with the first script "uploadFile.php" is actually supposed to do? And thats where I am @ present---"VIEWING THE INSERTED IMAGE? IMMEDIATELY AFTER INSERT?"
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 06:20 AM

Is file_id supposed to be an auto-increment number? You've supplied '' for it in the insert statement.

You are also trying to retrieve an image with id of 5. Check the database to see what numbers (file_id's) the images have.

You could also add
error_reporting(E_ALL);
ini_set('display_errors', '1');

to the very top of your scripts so that you can see all errors during development.
Was This Post Helpful? 0
  • +
  • -

#14 erabxes   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-September 14

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 06:35 AM

Yes! file_id is actually an auto-incremented id. And already, i do have images that are actually up to 4- id wise and the next image to be inserted is actually coming in with an id of 5.


Let me also add the error message code that you asked me to add to the top to see any possible development errors.
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,764
  • Joined: 12-June 08

Re: How to upload Multiple files into the database

Posted 17 September 2014 - 06:56 AM

Moving to PHP.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2