473,769 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Image Upload to Server & Name to Databse

ribbo
9 New Member
What I need in a nutshell is a form that will allow me to upload multiple pictures (say 5) to my server while at the same time saving the names to the MySQL database. I would prefer it if the image name was automatically changed with MD5 or similar before upload and entry, but is not so important as I can add at a later date. I also would like (on the same form) the ability to add other information. i.e. make, model, etc.

Example:

Image 1: [Browse]
Image 2:
Image………

Make:
Model…………

If there is anything anyone can do to help I would really
appreciate it.
Jan 16 '08 #1
16 9765
stepterr
157 New Member
What I need in a nutshell is a form that will allow me to upload multiple pictures (say 5) to my server while at the same time saving the names to the MySQL database. I would prefer it if the image name was automatically changed with MD5 or similar before upload and entry, but is not so important as I can add at a later date. I also would like (on the same form) the ability to add other information. i.e. make, model, etc.

Example:

Image 1: [Browse]
Image 2:
Image………

Make:
Model…………

If there is anything anyone can do to help I would really
appreciate it.
Ribbo,
Check out this site for a free script to get you started, Multiple File Upload Form . I used this as an example for myself and added more fields to the form for information that would be added to my database and then within the for loop and after the file was sucessfully uploaded I did my database INSERT statement. Hope that helps!
Jan 16 '08 #2
ribbo
9 New Member
Thanks,

I will check it out :)
Jan 17 '08 #3
ribbo
9 New Member
unfortunately that didn't help to much (maybe its just me)
I know some basic php and some advanced php. Although i am very patchy as i am self taught.

I would be truly grateful to anyone who can resolve my problem.

Here is the form.html page
Expand|Select|Wrap|Line Numbers
  1. <form enctype="multipart/form-data" action="add.php" method="post">
  2. Upload Image: <input name="userfile" type="file">
  3. <input type="submit" value="Upload File"></form>
  4.  
Here is the add.php page
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $username = "";
  4. $password = "";
  5. $database = "";
  6. $hostname = "";
  7.  
  8. mysql_connect($hostname,$username,$password);
  9. mysql_select_db($database) or die( "Unable to select database");
  10.  
  11. if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB. Please reduce the file size.<BR>";
  12. $file_upload="false";}
  13.  
  14. if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif"))
  15. {echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
  16. $file_upload="false";
  17. exit;}
  18.  
  19. function findexts ($filename) 
  20. {
  21. $filename = strtolower($filename) ; 
  22. $exts = split("[/\\.]", $filename) ; 
  23. $n = count($exts)-1; 
  24. $exts = $exts[$n]; 
  25. return $exts; 
  26. }
  27.  
  28. $ext = findexts ($_FILES['userfile']['name']) ;
  29. $ran = rand () ;
  30. $ran2 = $ran.".";
  31. $add = "upload/";
  32. $add = $add . $ran2.$ext;
  33.  
  34. if(move_uploaded_file($_FILES['userfile']['tmp_name'], $add)){
  35.  
  36. $query = "INSERT INTO car (image)". "VALUES ('$ran2$ext')";
  37. mysql_query($query) or die('Database Query Error!');
  38.  
  39. echo "Successfully uploaded the image";
  40. chmod("$add",0777);
  41. }
  42. else{echo "Failed to upload file Contact Site admin to fix the problem";
  43. exit;}
  44.  
  45. ?>
  46.  
As you can see i created this script so i can upload an image to the server, and the name to the database.

what i now need is the ability to upload multiple images at the same time, with the same form.

Thanks
Jan 17 '08 #4
stepterr
157 New Member
hi ribbo,

Have you used a for loop before? That's basically the only thing you are missing in your code to make it possible for you to do multiple files and its what that scripted I referenced earlier uses. The input field for type='file' will not let you select more than one file per input field so you'll need to either hard code in an input box for each file you want the user to be able to upload, or you can do it dynamically with a for loop.

Then when you get to your add.php page you'll just need another for loop to go through your steps for each file.

Does that clear things up a little bit?
Jan 17 '08 #5
Markus
6,050 Recognized Expert Expert
hi ribbo,

Have you used a for loop before? That's basically the only thing you are missing in your code to make it possible for you to do multiple files and its what that scripted I referenced earlier uses. The input field for type='file' will not let you select more than one file per input field so you'll need to either hard code in an input box for each file you want the user to be able to upload, or you can do it dynamically with a for loop.

Then when you get to your add.php page you'll just need another for loop to go through your steps for each file.

Does that clear things up a little bit?
No! Write to whole code for him.

NOW!
Jan 17 '08 #6
ribbo
9 New Member
I dont mind hard coding in an input field for each one, i would actually prefer to.
I just dont know how to make it work.

Sorry to anyone who is offended by me asking for help!
i am still new to php, but if it makes any difference i am reading and learning all i can.

i am sure in the near future i will also be helping others with there problems.
Jan 17 '08 #7
Markus
6,050 Recognized Expert Expert
I dont mind hard coding in an input field for each one, i would actually prefer to.
I just dont know how to make it work.

Sorry to anyone who is offended by me asking for help!
i am still new to php, but if it makes any difference i am reading and learning all i can.

i am sure in the near future i will also be helping others with there problems.
I was only joking, friend!

Say you had 3 input fields, yes? You name them 'name="file[]" ' that would then create an array out of the files.
On the upload page, you'd run through them with a foreach loop executing the code.

I've never actually done a multiple file upload... something i shall do right now!
Jan 17 '08 #8
stepterr
157 New Member
I was only joking, friend!

Say you had 3 input fields, yes? You name them 'name="file[]" ' that would then create an array out of the files.
On the upload page, you'd run through them with a foreach loop executing the code.

I've never actually done a multiple file upload... something i shall do right now!
Markusn00b is correct. You just need to make a few slight changes to accomplish this. I typically use a for loop when I create my input fields in situations like this so what I would do is something like this for the 5 fields you want :
[PHP]for($i=0; $i < 6; $i++)
{

echo "Upload field ".$i." &nbsp;<input name=\"userfile[]\" type=\"file\" id=\"userfile[]\" /><br />\n";
}[/PHP]

Now if you want to do this in HTML and not php and hard code each one then you may be able to do this for all 5 of your fields, but don't hold me to this because I haven't tried it yet.

[HTML]Upload Image: <input name="userfile[0]" type="file">
Upload Image: <input name="userfile[1]" type="file">
Upload Image: <input name="userfile[2]" type="file">[/HTML]


Now for the add.php page. I would take your function findexts and move it right below your database parameters since it can be called outside the for loop. Then take the rest of the code and place it in the for loop that will look like this:

[PHP]for($i=0; $i < 6; $i++)
{
//all of your upload code
}[/PHP]

Now how do you actually make sure that all of the files reference the correct name you ask? Well, for that you'll need to make use of the $i. So everywhere you are using the $_FILES['userfile']['name'] you'll need to add it like this:

[PHP]$_FILES['userfile']['name'][$i][/PHP]


Ok, after all of that you should be good to go.
Jan 17 '08 #9
stepterr
157 New Member
No! Write to whole code for him.

NOW!

See, you should've gone to bed when you said you were going to. Now you are just getting testy! ;-)
Jan 17 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
11763
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
0
1804
by: doffer | last post by:
I want to make a portfoliosystem where user can register and get their own portfolio... I've started the developer work, but I'm stuck on the image upload part... I'm experiencing some problems getting the picture resized and thumbnailed... I'm on a apache server running php 5 with 8MB php_memory. When uploading, the script works fine most times when uploading small files (below 100kb and sometimes up close to 300kb too), but when...
1
2509
by: John Thompson | last post by:
We're sooo close. When we load the page to upload the image, all of the prms go through except the binary image data. Using SQL server with the data type set to "image". Please help! Thanks- John
0
2835
by: dann2 | last post by:
hello, i try to upload in an access db two pictures at the same time. i use the adjusted sample code from persits. it looks like this: ... '<% ' Create an instance of AspUpload object 'Set Upload = Server.CreateObject("Persits.Upload") ' Capture uploaded file. Save returns the number of files uploaded 'Count = Upload.Save(Path) 'If Count = 0 Then 'Response.Write "message"
7
17058
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir = "photo/thumbs/"; // Path To Thumbnails Directory $twidth = "125"; // Maximum Width For Thumbnail Images
3
2469
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly the binary data for the file. I can sucessfully take the files out of the databse again and display them in a data grid. I would like to resize the submitted file to a fixed size (say 180 x 120) before I upload it to the database and do this without...
1
2036
by: sravani1 | last post by:
This code runs like when i submit the form it takes the image and displayed and top of the image a map will displayed. But actually i want that when i give the image it checks the location in the map and after displayed it.plz tell that how to start the logic. <?php // Connect to database $errmsg = "";if (! @mysql_connect("localhost","root","sreeni")) { $errmsg = "Cannot connect to database"; } @mysql_select_db("my_db1");...
8
9861
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the page coding) does verify file size, if correct it places it on the server in the users folder and with the correct names and extension, creates the thumbnail and adds the correlating file name. However, it only adds the first file name to the database....
1
1475
by: sajjad525 | last post by:
Any one please help me :i have upload the multiple image on server but i have problem to save the name of these image's name my code as following ... (test1.php) <? include_once('connection_db.php'); $username=$_REQUEST; $index=$_REQUEST; if($_REQUEST){ for($i=1;$i<3;$i++){ $filename= $_FILES; move_uploaded_file($_FILES, "../upload/".$filename);
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.