2

Trying to upload multiple files have different input names. Since I need only 1 insert query I need to upload them in an array I think. I need to insert file names to different columns in the same row

if(isset($_FILES['MainImage'])){
  $main_image_name = $_FILES['MainImage']['name'];
  $main_image_size = $_FILES['MainImage']['size'];
  $main_image_tmp = $_FILES['MainImage']['tmp_name'];
  $uploadMainTo = $uploadLocation.$main_image_name;
  $moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);

}

if(isset($_FILES['PDF'])){
  $pdf_name = $_FILES['PDF']['name'];
  $pdf_size = $_FILES['PDF']['size'];
  $pdf_tmp = $_FILES['PDF']['tmp_name'];
  $uploadPdfTo = $uploadLocation.$pdf_name;
  $movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);
}

My form looks like:

<input type="file" name="PDF">
<input type="file" name="MainImage">

Query:

$query = $db->execute("INSERT INTO users (pdf=?, main_image=?) VALUES (?,?) WHERE ID=$user_id", array($pdf, $main_image) );
4
  • You need only 2 files (1 PDF and 1 MainImage) or multiple (N PDFs and N MainImages)? Commented Jun 21, 2015 at 14:41
  • I need to insert 2 images (2 input fields) and 1 pdf file at the same time. Commented Jun 21, 2015 at 14:42
  • If you need help with your SQL Query too, please provide us some DB information and the query you have build so far... Commented Jun 21, 2015 at 14:42
  • Just updated my question Commented Jun 21, 2015 at 14:49

2 Answers 2

3

As you said in your comment:

I need to insert 2 images (2 input fields) and 1 pdf file at the same time

So, I assume that you will have all the file inputs in your HTML markup, something like this:

<input type="file" name="PDF">
<input type="file" name="MainImage">
<input type="file" name="SecondImage">

In this case, to insert the file path of the uploaded file to the database, something like this should work:

$uploadMainTo = null;
if(isset($_FILES['MainImage'])){
  $main_image_name = $_FILES['MainImage']['name'];
  $main_image_size = $_FILES['MainImage']['size'];
  $main_image_tmp = $_FILES['MainImage']['tmp_name'];
  $uploadMainTo = $uploadLocation.$main_image_name;
  $moveMain = move_uploaded_file($main_image_tmp,$uploadMainTo);
}

$uploadSecondTo = null;
if(isset($_FILES['SecondImage'])){
  $second_image_name = $_FILES['SecondImage']['name'];
  $second_image_size = $_FILES['SecondImage']['size'];
  $second_image_tmp = $_FILES['SecondImage']['tmp_name'];
  $uploadSecondTo = $uploadLocation.$second_image_name;
  $moveSecond = move_uploaded_file($second_image_tmp,$uploadSecondTo);
}

$uploadPdfTo = null;
if(isset($_FILES['PDF'])){
  $pdf_name = $_FILES['PDF']['name'];
  $pdf_size = $_FILES['PDF']['size'];
  $pdf_tmp = $_FILES['PDF']['tmp_name'];
  $uploadPdfTo = $uploadLocation.$pdf_name;
  $movepdf = move_uploaded_file($pdf_tmp,$uploadPdfTo);
}

$query = $db->execute("INSERT INTO users (pdf, main_image, second_image) VALUES (?,?,?) WHERE ID = ?", array($uploadPdfTo, $uploadMainTo, $uploadSecondTo, $user_id) );

I'm just not sure if i have to initialize the $uploadXTo variables with null or 'NULL'. If you have problems please test this way.

Sign up to request clarification or add additional context in comments.

Comments

1

Firstly correct your html

<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />

then you can use php array

$_FILES['userfile']['name'][0]
$_FILES['userfile']['tmp_name'][0]
$_FILES['userfile']['size'][0]
$_FILES['userfile']['type'][0]

Check the php docs: Uploading multiple files

5 Comments

What if I need to insert file names to different colomns in the same row in my database?
it's simple, $_FILES['userfile']['name'][0] is name of first file, and $_FILES['userfile']['name'][1] second file name
@Jawlon Rodriguez, can we use your php code with foreach loop
@user123 Yes you can.
@Jawlon Rodriguez, can you please update your answer with foreach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.