Im trying to upload files and update a page via frontend but I continue getting the message “The media type for “file.pdf” cannot be detected”, ¿what am I doing wrong?
controllers/request.php
<?php
return function ($kirby, $page) {
$alerts = [];
$success = -1;
if ($kirby->request()->is('POST') === true && get('upload')) {
$success = 0;
$pdf = $kirby->request()->files()->get('pdf');
$xml = $kirby->request()->files()->get('xml');
$kirby->impersonate('kirby');
try {
$pdfFile = $page->createFile([
'source' => $pdf['tmp_name'],
'filename' => crc32($pdf['name'].microtime()). '_' . $pdf['name'],
'template' => 'invoice',
'content' => [
'date' => date('d/m/Y h:m')
]
]);
$xmlFile = $page->createFile([
'source' => $xml['tmp_name'],
'filename' => crc32($xml['name'].microtime()). '_' . $xml['name'],
'template' => 'invoice',
'content' => [
'date' => date('d/m/Y h:m')
]
]);
//Upload de 'history' structure field
$history = $page->history()->yaml();
array_unshift($history, [
'pdf' => $pdfFile,
'xml' => $xmlFile
]);
$history = yaml::encode($history);
$page->update(array('history' => $history));
$success = 1;
} catch (Exception $e) {
$alerts['error'] = $e->getMessage();
}
}
return [
'alerts' => $alerts,
'success' => $success
];
};
Form
<form action="<?= $page->url() ?>" method="POST" enctype="multipart/form-data">
<label for="pdf">PDF</label>
<input type="file" name="pdf" id="pdf" accept="application/pdf" required>
<label for="xml">XML</label>
<input type="file" name="xml" id="xml" accept="text/xml" required>
<input type="submit" name="upload" value="Enviar">
</form>
I guess you need allow PDF file type from page blueprint:
I just add in my page blueprint
files:
accept: text/xml, application/pdf
But still not working
I couldn’t understand exactly where in the blueprint you are using it. You should add to file template blueprint.
# /site/blueprints/files/invoice.yml
title: Invoice
accept:
mime: text/xml, application/pdf
It can help to define mime types in htaccess. Im afraid i dont know the equivalent in Nginx.
These rules are a good start…
I put the wrong piece, but my files/invoice.yml looks similar
title: Invoice
accept:
mime: text/xml, application/pdf
maxsize: 3000000
I tried to do it without the template but without success.
So, can you upload a PDF file from the panel?
Yes, from the panel I uploaded the pdf and xml without problems
jahasielserna:
from the panel
With the same file template?
I’m trying to do it without the file template and I get the same error
The page where I try to create the files looks like this
title: Solicitud
preset: page
pages: false
fields:
response:
label: Estatus
type: select
width: 1/4
default: pending
options:
pending: Pendiente
resolved: Enviado
rejected: Rechazada
date:
label: Fecha
type: date
width: 1/4
default: today
history:
label: Historial
type: structure
fields:
sended:
label: Enviado
type: date
default: today
width: 1/4
pdf:
label: PDF
type: files
max: 1
width: 1/4
xml:
label: XML
type: files
max: 1
width: 1/4
comments:
label: Comentarios
type: text
businessName:
label: Razón social
type: text
width: 1/3
rfc:
label: RFC
type: text
width: 1/3
clientMail:
label: Correo del cliente
type: email
width: 1/3
texnixe
October 25, 2021, 5:51pm
11
Have you tried with different pdf files?
Yes, and all of them show the same error
This is the full error log, seems like it does not matter the file type, mime type is always missing
Error:
Kirby\Exception\InvalidArgumentException: The media type for "1526628088_factura2.xml" cannot be detected in C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\FileRules.php:186 Stack trace: #0 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\FileRules.php(62): Kirby\Cms\FileRules::validMime(Object(Kirby\Cms\File), '') #1 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\FileActions.php(114): Kirby\Cms\FileRules::create(Object(Kirby\Cms\File), Object(Kirby\Image\Image)) #2 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\FileActions.php(203): Kirby\Cms\File->commit('create', Array, Object(Closure)) #3 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\HasFiles.php(67): Kirby\Cms\File::create(Array) #4 C:\MAMP\htdocs\loyalplanet-facturas\site\controllers\request.php(21): Kirby\Cms\Page->createFile(Array) #5 [internal function]: Kirby\Cms\App->{closure}(Object(Kirby\Cms\App), Object(Kirby\Cms\Page)) #6 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Toolkit\Controller.php(49): Closure->call(Object(Kirby\Cms\App), Object(Kirby\Cms\App), Object(Kirby\Cms\Page)) #7 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\App.php(375): Kirby\Toolkit\Controller->call(Object(Kirby\Cms\App), Array) #8 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\Page.php(347): Kirby\Cms\App->controller('request', Array, 'html') #9 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\Page.php(1153): Kirby\Cms\Page->controller(Array, 'html') #10 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\App.php(597): Kirby\Cms\Page->render() #11 C:\MAMP\htdocs\loyalplanet-facturas\kirby\src\Cms\App.php(925): Kirby\Cms\App->io(Object(Kirby\Cms\Page)) #12 C:\MAMP\htdocs\loyalplanet-facturas\index.php(5): Kirby\Cms\App->render() #13 {main}
kirby\src\Cms\FileRules.php
185 if (empty($mime)) {
186 throw new InvalidArgumentException([
187 'key' => 'file.mime.missing',
188 'data' => ['filename' => $file->filename()]
189 ]);
190 }
texnixe
October 25, 2021, 9:28pm
14
Are you testing this locally or on a remote server?
Which exacty Kirby version are you using? Which PHP version? If locally, what setup?
Locally
Kirby 3.3.6
PHP 7.4.1
Server: Apache/2.2.31 (Win32)
Windows 10 x64
MAMP 4.2.0.23973
texnixe
October 25, 2021, 10:14pm
16
jahasielserna:
Kirby 3.3.6
That’s an outdated version. Please try with the current version.
Using the latest version is the best solution, I just checked and the problem seems to be fixed in 3.5. Thank you all for your support.
If anyone is having the same issue and cannot update his version, it can works changing line 62 in kirby\src\Cms\FileRules.php
62 static::validMime($file, $upload->mime());
to
62 static::validMime($file, $upload->mime() ?? $file->mime());