ich hab mal eine Frage in Bezug auf Dateienupload und Datenbanken. Genauer möchte ich aus einem Formular im Auswahlfenster mehrere Dateien auswählen und hochladen. Der Pfad zu den Bildern/Dateien soll in "einem" Datenbankeintrag mit einer ID gespeichert werden.
Mir ist es gelungen mehrere Daten in ein Verzeichnis "upload" hochzuladen, allerdings erzeugt die DB für jede Datei einen separaten Eintrag. Allerdings möchte ich die Bilder alle von "einer" id mit $Get_id holen, beim ausgeben/ändern.
Hier das Formular:
Code: Alles auswählen
<form action="include/bild2.inc.php" method="post" enctype="multipart/form-data">
Wähle Dateien für Upload:
<input type="file" name="files[]" multiple >
<input type="submit" name="submit" value="UPLOAD">
</form>
Code: Alles auswählen
<?php
if(isset($_POST['submit'])){
// Include the database configuration file
include_once 'dbh.inc.php';
// File upload configuration
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif','JPG','php');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
$fileNames = array_filter($_FILES['files']['name']);
if(!empty($fileNames)){
foreach($_FILES['files']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['files']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
$insertValuesSQL .= "('".$fileName."', NOW()),";
}else{
$errorUpload .= $_FILES['files']['name'][$key].' | ';
}
}else{
$errorUploadType .= $_FILES['files']['name'][$key].' | ';
}
}
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL, ',');
// Insert image file name into database
$insert = $conn->query ("INSERT INTO berichte (file_name,uploaded_on) VALUES $insertValuesSQL");
if($insert){
$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
$statusMsg = "Files are uploaded successfully.".$errorMsg;
header ("Location:../diagdat3.php?Entry=naechster-Schritt-bitte!");
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
}
?>
Welchen Befehl muss ich ausgeben? Wäre klasse wenn Ihr mir helfen könntet.
Vielen Dank im vorraus für Eure Hilfe.