O have html5 Form that allows upload of multiple images. I want that save images in an upload folder and store the name of the files in a database. Every column in the database can have more image. So I have created a new table allegato
where I save name of image and id of issue that it represents. I also have segnalazione
table where I have the report. So several images refer to one report.
I can upload multiple images to the server, but I don't know as save for every image his name, so as to associate, in the table of the db, their signaling.
In segnalazione.php
I have this function to upload report. At the end of the file I can associate the report to the "attachment" table, but I don't know how to add the link for each file loaded at the same time.
function create()
{
$query=" INSERT INTO ". $this->table_name ." SET
nome=:nome, descrizione=:descrizione, contatto=:contatto, tipo_segnalazione=:tipo_seg, indirizzo=:via1, ip=:ip, tracking=:track";
$stmt = $this->conn->prepare($query);
// bind values
$stmt->bindParam(":nome", $this->nome);
$stmt->bindParam(":descrizione", $this->descrizione);
$stmt->bindParam(":contatto", $this->contatto);
$stmt->bindParam(":tipo_seg", $this->tipo_seg);
$stmt->bindparam(":via1",$this->via1);
$stmt->bindparam(":ip",$this->ip);
$stmt->bindparam(":track",$this->tracking);
if($stmt->execute()){
$stmt1 = $this->conn->prepare("SELECT id FROM coratocivile where tracking=:track");
$stmt1->bindparam(":track",$this->tracking);
$stmt1->execute();
$id = $stmt1->fetch();
$id1= $id['id'];
$stmt2 = $this->conn->prepare("insert into allegato set id_segnalazione='$id1'");
$stmt2->execute();
}else{
return false;
}
return true;
}
in the index.php where i upload image have:
if($_POST){
$segnalazione->descrizione=$_POST['descrizione'];
$segnalazione->contatto=$_POST['contatto'];
$segnalazione->nome=$_POST['nome'];
$segnalazione->tipo_seg=$_POST['tipseg'];
$segnalazione->ip=$ip = $_SERVER['REMOTE_ADDR'];
$segnalazione->via1=$_POST['via'];
$segnalazione->tracking=time();
$file_dir = "uploads";
function compress_image($source_url, $destination_url, $quality) {
if($source_url==NULL)
{
echo "<div class='alert alert-danger'> Caricamento allegato fallito. Assicurati di aver allegato una foto o un video!</div>";
return null;
}
else {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source_url);
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
}
if (isset($_POST["singlebutton"])) {
for ($x = 0; $x < count($_FILES['file']['name']); $x++) {
$file_name = time() . $_FILES['file']['name'][$x];
$file_tmp = $_FILES['file']['tmp_name'][$x];
/* location file save */
$file_target = $file_dir . DIRECTORY_SEPARATOR . $file_name;
try {
if( $filename = compress_image($file_tmp, $file_target, 30) && $segnalazione->create()){
echo "<div class='alert alert-success'> Segnalazione inviata. </div>";
}
else echo "<div class='alert alert-danger'> Segnalazione non inviata. Assicurati di aver compilato tutti i campi corretttamente. </div>";
} catch (Exception $e) {
echo "<div class='alert alert-danger'> Caricamento allegato fallito </div>";
}
}
}
}
?>