1

I want to upload and store multiple files/pics path in database with 1 submit button ...... This is what i am doing in html form

Icon:
<br>
<br>
<input type="file"     name="uploadedfile" >
<br>
<br>
Screenshot:
<br>
<br>
<input type="file"  name ="fileToUpload"> 
<br>

And this is my php code

<?php 

if (isset($_POST['submit'])){
$target_dir= "images/";
$target_file= $target_dir.basename($_FILES['uploadedfile']['name']);
$tmp=$_FILES['uploadedfile']['tmp_name'];
if (move_uploaded_file($tmp,$target_file) ){

    echo "uploaded successfully";

}
else {
    echo "not uploaded successfully";
}

$targets_dir= "images/";
$targets_file= $targets_dir.basename($_FILES['fileToUpload']['name']);
$tmps=$_FILES['fileToUpload']['tmp_name'];

if (move_uploaded_file($tmps,$targets_file) ){

    echo "uploaded successfully";

}
else {
    echo "not uploaded successfully";
}

$insert=" insert into app values  (DEFAULT,'$Title', '$target_file' , '$targets_file'  )";

}

But its not working ...... Any advice will be appreciated .. Thanks in advance

  • Write a dump of your $_FILES variable on the first line and maybe you will see the pattern... – Cosmin Aug 31 '15 at 14:25
  • Also, `$_FILES[...]['name']` does not contain a path but only the filename. Using `basename()` could actually be a problem here. I also would advise against just assuming something was uploaded and instead explicitly checking if the elements in `$_FILES` exist and don't report an error. One last thing: Is your form correctly set to `enctype="multipart/form-data"`? – Till Helge Aug 31 '15 at 14:28
  • and 1 thing more when i tried to upload only 1 pic it works fine ... But when i tried to upload 2 pics then nothing happened...@Till Helge – zeeshan sarfraz Aug 31 '15 at 14:32

2 Answers2

0

You can do multiple file uploads from a single input element.

For your case, you can just rename both of your input element's name to be something like file[]. The array ([]) is important and tells that file is really an array of multiple files.

On the server side, the first file name (your icon) can be accessed like $_FILES['file']['name'][0] and the second name (the screenshot) like $_FILES['userfile']['name'][1].

You can review more options in the documentation.

Community
  • 1
  • 1
Leroy
  • 237
  • 2
  • 11
0

Use this code, u just need to insert the query. Rest everything will work fine.

 if ((!empty($_FILES['uploadedfile']["name"])) && (!empty($_FILES['fileToUpload']["name"])))
{

$file_name1=$_FILES['uploadedfile']["name"];
$temp_name1=$_FILES['uploadedfile']["tmp_name"];
$imagename1=date("d-m-Y")."-".time();       
$target_path1 = "images/".$imagename1;
$Rtarget_path1 = "images/".$imagename1;

$file_name2=$_FILES['fileToUpload']["name"];
$temp_name2=$_FILES['fileToUpload']["tmp_name"];
$imagename2=date("d-m-Y")."-".time();       
$target_path2 = "images/".$imagename2;
$Rtarget_path2 = "images/".$imagename2;

   if((move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $Rtarget_path1 )) && (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $Rtarget_path2 )))
    {
        $insert=" insert into app values  (DEFAULT,'$Title', '$target_path1' , '$target_path2'  )"; // Insert Query Wrong. Also Check Here
    }
}
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
  • Thanks for your comment .... But Actually my problem is this that when i tried to upload 2 pics then it seems that php code does not execute .... but when i tried to upload 1 pic then everything happened just fine – zeeshan sarfraz Aug 31 '15 at 14:43
  • when you try to upload 2 images from one file ? Are you uploading two images from single input-file @zeeshansarfraz – Nana Partykar Aug 31 '15 at 14:46
  • Icon:



    Screenshot:


    you can see i am using 2 inputs .......Both have different names @Danish Enam
    – zeeshan sarfraz Aug 31 '15 at 14:50
  • yess.. i can see that. and, can well understand Mr @zeeshansarfraz check my edited answer. – Nana Partykar Aug 31 '15 at 14:56
  • Its not working ...... @Danish Enam i think that when i tried to upload 2 pics that php code does not execute .. i dont know why its happening but this is sure that after submitting 2 pics php code is not executing – zeeshan sarfraz Aug 31 '15 at 15:18