Miuranga
Miuranga

Reputation: 2463

How To Upload two files in PHP

I'm trying to upload two file in one submit button using the following code:

<label>Logo Image *</label>
<input type="file" name="ufile[]"/>
<label>Banner Image *</label>
<input type="file" name="ufile[]"/>

PHP

$logo = $_FILES['ufile']['name'][0];
$block_img = $_FILES['ufile']['name'][1];

 if ($_FILES['ufile']['name']["error"] > 0) {
    echo "error<br>";
   }
  else {
    if (file_exists("small-image/" .  $_FILES['ufile']['name'][0])){
        echo $_FILES['ufile']['name'][1] . "File already exists in server. ";
    }
    else {
        move_uploaded_file($_FILES['ufile']['name'][0], "small-image/" . $_FILES['ufile']['name'][0]);
        move_uploaded_file($_FILES['ufile']['name'][1], "small-image/" . $_FILES['ufile']['name'][1]);
    }
 }

$sql_query = "UPDATE header_img SET logo_img = '$logo', block_img = '$block_img' WHERE banner_id = 1";

My database is updating correctly but the file is not uploaded. Yes there is a 777 directory call 'small-image'.

Any idea? Thanks.

Upvotes: 0

Views: 390

Answers (1)

gen_Eric
gen_Eric

Reputation: 227240

When you use move_uploaded_file, you want to use $_FILES['ufile']['tmp_name'], that's where the file is currently located.

move_uploaded_file($_FILES['ufile']['tmp_name'][0], "small-image/" . $_FILES['ufile']['name'][0]);
move_uploaded_file($_FILES['ufile']['tmp_name'][1], "small-image/" . $_FILES['ufile']['name'][1]);

Check the example in the docs: http://php.net/manual/en/function.move-uploaded-file.php

Upvotes: 1

Related Questions