Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created July 1, 2016 16:14
Show Gist options
  • Save arthtilva/e38fa6019832f39214ad7a854f001338 to your computer and use it in GitHub Desktop.
Save arthtilva/e38fa6019832f39214ad7a854f001338 to your computer and use it in GitHub Desktop.
Multiple File upload
<?php
$conn = mysqli_connect("localhost","dinesh","_[#*cch(LpEB","dinesh_accident_report") or die(mysql_error());
$street=$_REQUEST['street'];
$place=$_REQUEST['place'];
$description=$_REQUEST['description'];
$latitude=$_REQUEST['latitude'];
$longitude=$_REQUEST['longitude'];
$date=$_REQUEST['date'];
$images="";
$c=1;
foreach($_FILES as $file){
$target_path = "accident_images/";
$pieces = explode(".", $file['name']);
$base_name= $pieces[0].time().".".$pieces[1];
$target_path = $target_path . basename($base_name);
if(move_uploaded_file($file['tmp_name'], $target_path)) {
$images=$images.$base_name.";";
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename($base_name);
echo "target_path: " .$target_path;
}
$c++;
}
if(strlen($images)>0){
$images=substr($images, 0, -1);
}
$sql = "INSERT INTO accident (street, place, description,images,latitude,longitude,date) VALUES ('$street', '$place', '$description','$images',$latitude,$longitude,'$date')";
if ($conn->query($sql) === TRUE) {
$sMessage= "New accident posted successfully";
$last_id = $conn->insert_id;
$toPrint[] =array(
"sStatus" => true,
"sMessage" => $sMessage
);
} else {
$sMessage="Error in generating new accident.";
$toPrint[] =array(
"sStatus" => false,
"sMessage" => $sMessage
);
}
echo json_encode($toPrint);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment