Sam San
Sam San

Reputation: 6903

Two or more file input

I have more than one input which are of type file

<form  name="vform" method="post" enctype="multipart/form-data" >
  <input type=file name='thumbs1' />
  <input type=file name='thumbs2' />
  <input type=file name='thumbs3' />
  ... etc
</form>

let say i got the count of file input=3

$i = 1;
while($i <= 3)
{
   $thumb = "thumbs".$i;
   $fileName = $_FILES['$thumb']['name'];
   $tmpName  = $_FILES['$thumb']['tmp_name']; 
   $filePath = "directory/";

   if(move_uploaded_file($tmpName, $filePath)){echo " ";}else{echo " ";}

   $i++;
}

What happens here is that only the first one got the filename, but not one of them move to the expected directory or anywhere else.

Is there a specific way to declare when dealing with multiple file input?

Upvotes: 0

Views: 164

Answers (2)

Muthu Kumaran
Muthu Kumaran

Reputation: 17900

Remove the single quotes from $_FILES['$thumb'] and change it to $_FILES[$thumb]

PHP will not parse variables within the single quotes ' '.

On move_uploaded_file function you need to specify the file name for destination before moving

$i = 1;
while($i <= 3)
{
   $thumb = "thumbs".$i;
   $fileName = $_FILES[$thumb]['name'];
   $tmpName  = $_FILES[$thumb]['tmp_name']; 
   $filePath = "directory/";

   if(move_uploaded_file($tmpName, $filePath.$fileName)){echo " ";}else{echo " ";}

   $i++;
}

Upvotes: 2

senK
senK

Reputation: 2802

Maybe the single quotes in the files will be the problem, try out this

$i = 1;
while($i <= 3)
{
   $thumb = "thumbs".$i;
   $fileName = $_FILES[$thumb]['name'];
   $tmpName  = $_FILES[$thumb]['tmp_name']; 
   $filePath = "directory/".$filename;

   if(move_uploaded_file($tmpName, $filePath)){echo " ";}else{echo " ";}

   $i++;
}

Upvotes: 0

Related Questions