Hi,
ich hab in einem Formular welches per PHPMailer verschickt wird ein Datenupload für jpgs und PDFs.
Nun habe ich das Problem das wenn ich PDFs und Jpgs hochlade, wird nur manchmal nur eins der Dateien verschickt. Bei zwei JPGs bekomm ich bisher immer beide.
Ich verstehe nicht woran das liegen könnte:
ich hab in einem Formular welches per PHPMailer verschickt wird ein Datenupload für jpgs und PDFs.
Nun habe ich das Problem das wenn ich PDFs und Jpgs hochlade, wird nur manchmal nur eins der Dateien verschickt. Bei zwei JPGs bekomm ich bisher immer beide.
Ich verstehe nicht woran das liegen könnte:
HTML:
<div class="btn fileinput-button span-12">
<i class="gh-upload primary-circle"></i>
<!-- The file input field used as target for the file upload widget accept=".jpg,.png,.pdf,.gif "-->
<input id="fileupload" type="file" name="uploaded_files[]" multiple="multiples" accept=".pdf, .jpg">
<span class="filelabel span-4">Lageplan/Fotos <br>(pdf oder jpg) </span><span class="filesize span-12" style="font-size: 12px;">max. Dateigröße = 5 MB</span>
</div>
<ul id="files" class="files"></ul>
Javascript:
$("#fileupload").change(function(data) {
var files = $(this)[0].files;
for (var i = 0; i < files.length; i++) {
var fileType = files[i].type;
var ValidImageTypes = ["image/jpeg", "application/pdf"];
if ($.inArray(fileType, ValidImageTypes) < 0) {
$(".filelabel").after('<div class="alert span-7">' + fileType + 'Bitte laden Sie nur .jpg- oder .pdf-Dateien hoch!<button class="alertclose">×</button></div>');
}
else {
$("#files").append('<li class="m-6 span-4" data-path="'+ files[i].path +'">' + files[i].name + '</li>');
}
}
});
PHP:
try {
//Attach multiple files one by one
for ($i = 0; $i < count($_FILES['uploaded_files']['tmp_name']); $i++) {
if($_FILES['uploaded_files']['error'][0] !== 4){
if ($i == $maximumfiles) {
throw new RuntimeException('<div class="alert">Sie haben zuviele Dateien hochgeladen.</div>');
}
if (!isset($_FILES['uploaded_files']['error'][$i])) {
throw new RuntimeException('<div class="alert">Etwas mit Ihren Dateien stimmt nicht.</div>');
}
switch ($_FILES['uploaded_files']['error'][$i]) {
case UPLOAD_ERR_OK:
break;
// case UPLOAD_ERR_NO_FILE:
// throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint nicht verschickt worden zu sein.</div>');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint von der Datenmenge groß zu sein.</div>');
// default:
// throw new RuntimeException('<div class="alert">Ein unbekannter Fehler ist aufgetreten.</div>');
}
if ($_FILES['uploaded_files']['size'][$i] > 5000000) {
throw new RuntimeException('<div class="alert">Die maximale Dateigröße ist überschritten.</div>');
}
$extension = strtolower(pathinfo($_FILES['uploaded_files']['name'][$i], PATHINFO_EXTENSION));
$allowed_extensions = array('jpg', 'jpeg', 'pdf');
if(!in_array($extension, $allowed_extensions)) {
throw new RuntimeException('<div class="alert">Nicht erlaubtes Dateiformat!</div>');
}
$allowed_types = array ('application/pdf', 'image/jpeg');
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$detected_type = finfo_file( $fileInfo, $_FILES['uploaded_files']['tmp_name'][$i]);
if ( !in_array($detected_type, $allowed_types) ) {
throw new RuntimeException('<div class="alert">Nicht erlaubtes Dateiformat!</div>');
}
finfo_close( $fileInfo );
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['uploaded_files']['name'][$i]));
$filename = $_FILES['uploaded_files']['name'][$i];
if (move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$i], $uploadfile)) {
$files = $mail->addAttachment($uploadfile, $filename);
} else {
throw new RuntimeException('Failed to move file to ' . $uploadfile);
}
}
}