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
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
}
}
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.