0

I have a form.When a user clicks on add a file. Two input fields are appended. One with the input name of the file to be uploaded and another with input field for the file itself

<form>
  <label>File 1</label>
  <input type="text" name="fileName[]"/>
  <input type="file" name="file[]"/>
  
  <label>File 2</label>
  <input type="text" name="fileName[]"/>
  <input type="file" name="file[]"/>
  .
  .
  .
  .
  
  <input type="submit" value="submit"/>

How do i get the corresponding file name and the file together for insertion Since the text field will be stored in $_POST variable and file in $_FILES

Samuel James
  • 1,350
  • 12
  • 15
  • Have you taken a look at any of the answers? I see you have just joined this site. The way StackOverflow works is that feedback goes back and forth until you get grounded on the matters in questions. – Ifedi Okonkwo Oct 11 '15 at 17:12
  • Possible duplicate of [JavaScript post request like a form submit](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) – Jan Oct 11 '15 at 18:25

2 Answers2

2

First your form needs to have the attribute enctype="multipart/form-data" like so:

<form type="post" enctype="multipart/form-data">

On submit: you can get both $_FILES and $_POST arrays. To see what you have, try (if submitted):

echo '<pre>';
print_r($_POST);
print_r($_FILES);
echo '</pre>';

If your file field name attribute was "upload_pic", then your $_FILES array should look like:

Array
(
    [upload_pic] => Array
        (
            [name] => name_of_file
            [type] => file_type
            [tmp_name] => temporary_name
            [error] => 4
            [size] => 0
        )

)

I believe that gives you any info you need to insert to DB. (E.g. $filename = $_FILES['upload_pic']['name']), add it to the $_POST array and carry on with your insert query.

EDIT: Re-reading your question, I see:

When a user clicks on add a file. Two input fields are appended. One with the input name of the file to be uploaded and another with input field for the file itself...

I was wondering why you need to manually construct a separate text input for the "fileName", since you can obtain it from the $_FILES array (as explained above).

Ifedi Okonkwo
  • 2,878
  • 3
  • 23
  • 41
0

Store what is in $_POST and what is in $_FILES in variables. then you need to open de file with fopen and fwrite the to the file the variable you retrieved from $_POST. Don't forget fclose the file when you are done.

Hiltje
  • 140
  • 1
  • 5
  • I have
    as my form attribute . For each upload Users will be able to select the category of file he is uploading e.g(Photo, Document, Certificate), Am not looking for the file name itself But the category the file falls in to. Users add as many as possible. A click on add more button will append two input fields simultaneously (a select box and Input field for the file) How do i get the corresponding file category Name in the select box and the file together for insertion Since the text field is stored in $_POST variable and file in $_FILES
    – Samuel James Oct 12 '15 at 17:13