Know if it is an image or PHP link

0

I am writing a code that consists of reading files from a folder in which there are images and links, and I need to differentiate between image and link, but I have been very very caught. The code is this in case you can help me:

//Leer archivos de la carpeta
            $filehandle = opendir($ruta); // Abrir archivos de la carpeta
            //Bucle para seleccionar filas y columnas
            for($i=0; $i<$numFilas; $i++){
                $Visualizacion .= "<tr>";
                for($w=0; $w<$numColumnas; $w++){
                    $Visualizacion .= "<td>";
                        while($file = readdir($filehandle)){
                            if($file != "." && $file != ".."){
                                $tamanyo = GetImageSize($ruta . $file);
                                $rutaFile = "$ruta$file";

                                $Visualizacion .= "<td><a TARGET = '_top' href = '$ruta$file'><img src='$ruta$file' $tamanyo[3]></a></td>";

                                $patron = "/I_(.*)_IH_(.*)_HP_(.*)_PN_(.*)_N\./";
                                $result = preg_match($patron, $file, $resultados);

                                $nombreCompleto = $resultados[0];
                                $vecesMostrar = $resultados[1];
                                $horariosProhibidos = $resultados[2];
                                $periodo = $resultados[3];
                                $nombre = $resultados[4];
                                $rutaCompleta = "$ruta$file";

                                //Llamo a la función que se encarga de agregar los registros
                                agregarImpresiones($nombre,$ruta, $vecesMostrar, $horariosProhibidos, $periodo);
                                impresionesTotales($nombre, $ruta, $periodo, $vecesMostrar, $rutaCompleta);
                                break;
                            }//end if
                        }//end while
                    $Visualizacion .= "</td>";                          
                }//end for
                $Visualizacion .= "</tr>";
            }//end for

            closedir($filehandle); // Fin lectura archivos

With the filehandle I understand that it reads all the files, but how does it differentiate them? Thank you very much for the help!

    
asked by Csc99 22.05.2018 в 10:14
source

1 answer

5

In your case I think it would be best to look for the mimetype of each of the files you are analyzing. If you have in your text image / jpg, or image / gif ... (in general, if you have the word image) then it is an image file.

The mimetype you can look at it like this:

$finfo = finfo_open(FILEINFO_MIME_TYPE); // buscaremos mimetype
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);

finfo_open allows you to indicate the information you are going to search for in the file. To finfo_file you indicate the $ fine and the path of the file you want to analyze.

    
answered by 22.05.2018 / 11:41
source