Speichern von Dateien als Blob in der Datenbank ajax php pdo
$fileCount = count($_FILES);
for ($i = 0; $i < $fileCount; $i++) {
$fp = fopen($_FILES["file_".$i]['tmp_name'], 'rb');
$stmt4 = $dbh - > prepare("INSERT INTO files_tbl (pin,remarks,fileblob,file_type,nameoffile,filesize) VALUES (?,?,?,?,?,?)");
$stmt4 - > bindValue(1, $pin, PDO::PARAM_STR);
$stmt4 - > bindValue(2, $remarks, PDO::PARAM_STR);
$stmt4 - > bindParam(3, $fp, PDO::PARAM_LOB);
$stmt4 - > bindParam(4, $_FILES["file_".$i]['type'], PDO::PARAM_STR);
$stmt4 - > bindValue(5, $_FILES["file_".$i]['name'], PDO::PARAM_STR);
$stmt4 - > bindValue(6, $_FILES["file_".$i]['size'], PDO::PARAM_STR);
$stmt4 - > execute();
}
Dies ist, wie ich Datei als Blob in PHP einfügen. Es wird eine Datei gespeichert, aber nicht richtig. Wenn ich sage, es spart nicht richtig, dann ist auf dem Weg etwas nicht in Ordnung. Wenn ich das Speichern der Datei mit meinem Projekt und das manuelle Hinzufügen der Datei in XAMPP vergleiche, gibt es einen Unterschied in Fileblob. Zum Beispiel speichere ich eine Datei manuell in xampp. Das Fileblob ist [BLOB - 488.9 KiB]
Wenn ich das Projekt benutze, wird es zu[BLOB - 479.2 KiB]
. Ich denke, dass dies der Grund ist, wenn ich versuche, die Datei aus der Datenbank anzuzeigen. Es wird eine leere Seite angezeigt (wenn die angezeigte Datei die Datei ist, die ich mithilfe des Projekts einfüge), aber wenn die angezeigte Datei die Datei ist, die ich manuell in xampp einfüge es zeigt die Datei.
Was könnte in meinem Insert falsch sein? Warum speichere ich nicht den richtigen Blob
UPDATE
<input type="file" id="filecontent" name="filecontent" multiple="">
ajax
var file = $('#filecontent')[0].files;
for (var i = 0; i < file.length; i++) {
formData.append("file_" + i, file[i]);
//more data are passed to formData
//formData.append("file", file[i]);
console.log(file[i]);
}
$.ajax({
url: '../include/AddNew.php',
type: 'POST',
dataType: "json",
data: formData,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function(data) {
console.log(data);
alert(data.message);
//window.location.reload(true);
},
error: function(data) {
//alert("Error!"); // Optional
}
});
UPDATE
wenn ich versucht habe, @ zu setzprint_r($_FILES);
vor der Zeilefor ($i = 0; $i < $fileCount; $i++) {
die Ausgabe ist
Array
(
[file_0] => Array
(
[name] => whomovedmycheese - Copy.pdf
[type] => application/pdf
[tmp_name] => C:\Users\HogRider\xampp\tmp\phpE775.tmp
[error] => 0
[size] => 500624
)
[file_1] => Array
(
[name] => whomovedmycheese.pdf
[type] => application/pdf
[tmp_name] => C:\Users\HogRider\xampp\tmp\phpE786.tmp
[error] => 0
[size] => 500624
)
)
UPDATE