Upload a file to the server, limiting extensions and file type

0

With $_FILES["files"]["type"] I can upload files pdf , but I'm trying to specify that I also accept files .ai ( illustrator ) and other possible extensions, but I can not do it.

And will there be any way to visualize it on my page, as it is done with the pdf files?

My code:

 if($_FILES["files"]["type"][$i]=="application/pdf" || $_FILES["files"]["type"][$i]=="application/ai") {}
    
asked by Eduardo Ramirez 24.01.2018 в 18:11
source

2 answers

0

To validate your AI illustrator extension you can use application/postscript .

A possible example:

<?php 
if (isset($_POST['sumb'])) {

    //Obtenemos imagen.
    $file = $_FILES["files"];
    $file_name = $file["name"];         
    $file_type = $file["type"];

    //Comprobamos la extensión.         
    if ($file_type != "application/pdf" && $file_type != "application/postscript") {
        echo "Tu extensión no es valido";
    } else {
        echo 'Tu extensión es valido';
    }
}
?>      

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="files" />
    <input type="submit" name="sumb" value="Cargar" />
</form>


To your second question, how to show a PDF in a iframe .

You could create something like this:

<object data="ruta_pdf" type="application/pdf">
    <embed src="ruta_pdf" type="application/pdf" />
</object>

Source SO .

    
answered by 24.01.2018 / 18:51
source
0

The first question is solved in a simple way:

<!-- language: lang-html -->
<form method="POST" enctype="multipart/form-data">
    <input name="nombre_campo" type="file" accept="token_tipos_aceptados"
    multiple="1" /><!-- 1 para indicar que acepta varios archivos -->
    <input type="submit" name="btncargar" value="Cargar" />
</form>

Where token_tipos_aceptados must be a space-separated string of identifiers of supported file types, conforming to the html specification recommended by W3C . When you click on browse to find the files to load, the browser will only show files of the indicated types.

If multiple=1 is specified, the browser will accept loading several files simultaneously into a single input element. But, be careful because the superglobal $ _FILES passes them with a format of arrangement quite different from the usual.

    
answered by 18.06.2018 в 05:50