Hi, I have a form in which I upload a .PDF file. The problem is that sometimes, if it detects PDFs and other times other files with the same ending, it tells me that they are not of that type. What could I do? I leave here an extract of my code
<form action="upload_book_prueba.php" method="post" class="regisform" enctype="multipart/form-data">
<label>Subir el ebook </label>
<input type="file" name="ebook" />
<input type="submit" name="enviar" value="Upload">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST"){
extract($_POST);
$ebook=$_FILES['ebook']['tmp_name'];
if($_FILES['ebook']['type']=="application/pdf") {
$ruta_ebook="ebook/".$nombre_ebook.".pdf";
if(copy($ebook,$ruta_ebook)){
echo "Entra aqui";
}else{
echo "No entro";
}
}
}
NOTE: $ebook_name corresponds to a query of the max id of the ebooks table, and add 1 to that number.
Do not use
$_FILES['ebook']['type'] == "application/pdf"
for the following two reasons:application/octet-stream
).Instead you should use
mime_content_type()
and allow your server to detect the type by parsing the content of the uploaded file using your server's magic numbers (magic.mime
).Example with
mime_content_type()
:Example with
finfo::file()
:Increasing the size of received files
Ideally, modify the file
php.ini
and setupload_max_filesize
ypost_max_size
to a larger value.For example, to configure 32 MiB using notation
shorthand
, we would put:Another alternative option is to create a file
.htaccess
(if you use apache and its use is enabled) with the following content: