[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

multifile upload

I have a page with 6 file fields. My client can upload as many he wants..like 2 images or 6 images.

What I need is to check which field is not empty, upload these files and than, store the filename of each file into a database:

file1, file2, file3, file4, file5, file6

I'm using the following code to upload the files but I don't know how to check how many files were uploaded and how to insert each name in the right field into the database.
<?php
#send the photos
				if ($send=="yes") {	
					
					while(list($key,$value) = each($_FILES[image][name]))
					{
						if(!empty($value)) // this will check if any blank field is entered
						{   
							$filename = $value;  // filename stores the value
							$add = "uploads/$filename";  // upload directory path is set
							//echo $_FILES[image][type][$key];  // uncomment this line if you want to display the file type
							// echo "<br>"; // Display a line break
							copy($_FILES[image][tmp_name][$key], $add); //  upload the file to the server
						}
					}
						
					$upload = "sucess";		
						
				}
 
?>

Open in new window

0
fackz
Asked:
fackz
  • 5
  • 5
4 Solutions
 
nanharbisonCommented:
Try this file upload class:
http://www.phpclasses.org/browse/package/1708.html
it uploads files securely and allows you to configure the upload, like if you want to assign the files a random name (so no one can upload it and then go to it to mess with your website). It has some good examples of how to use it. You can allow more than one file to be uploaded.
(You have to register to download this code, but it is free.)
0
 
fackzAuthor Commented:
Hi nanharbison,

My problem is not in the file upload. My code works fine.
The problem is to know which files were uploaded, grab their names, and save each one into a column on my database.

but thanks for your attention!
0
 
nanharbisonCommented:
Sorry!


You are already grabbing the name of the images, so add a line which adds the row to the db table, and you need to have your db connection info available to this page (I am assuming you know how to do that)




     if(!empty($value)) // this will check if any blank field is entered
          {  
                    $filename = $value;  // filename stores the value
                    $add = "uploads/$filename";  // upload directory path is set
                //echo $_FILES[image][type][$key];  // uncomment this line if you want to display the file type
              // echo "<br>"; // Display a line break
             copy($_FILES[image][tmp_name][$key], $add); //  upload the file to the server
         $uploadquery = "INSERT into TABLENAME values..... etc
        $query_db = mysql_query($uploadquery) or die (mysql_error());
            }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nanharbisonCommented:
If you need  know how many images were uploaded, add a counter to this if statement.
Before the if statement:
$i = 0;
After the query, inside the if statement, add:

$i++;
which increments $i by one each time one image is uploaded.
0
 
fackzAuthor Commented:
Thanks again for your help....here is my question:

The insert is the problem, once I have 6 fields on my database how should I write it?

INSERT into TABLENAME (file1, file2, file3, file4, file5, file6) VALUES ???
0
 
nanharbisonCommented:
I need to know the name of the table this data will be inserted into and the names of all of the fields in the table
0
 
fackzAuthor Commented:
the name of the table is PHOTOS and I have these fields:
file1, file2, file3, file4, file5, file6
0
 
nanharbisonCommented:
Hmmm, are you aware that this table design breaks normalization of tables? Maybe it is too late to change this? Or can it be changed?
You would be much better off having a table with one file at a time, (and a field that is the foreign key with the ID of the user uploading the images) since there can be anywhere from 1 to 6 fields with values. If your website gets very busy with the way it is set up now, you will waste serious resources storing and retrieving values.
0
 
fackzAuthor Commented:
I didn't know that...thanks for your advice! But I can't change it right now...I'm working in this project for a client of the company that I work.
0
 
fackzAuthor Commented:
just add inside the while loop:

$na[] = "$add";      //array com o nome dos arquivos

than, build the sql query:

$sql2="INSERT INTO $tbl_name(titulo, autor, data, mes,foto,img2,img3,img4,img5,img6,img7,img8,img9)
                                    VALUES
                                    ('$titulo', '$autor', '$data', '$mes','$na[0]','$na[1]','$na[2]','$na[3]','$na[4]','$na[5]','$na[6]','$na[7]','$na[8]')";
0

Featured Post

Hire Technology Freelancers with Gigs

Work with 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.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now