Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active December 22, 2015 04:29
Show Gist options
  • Save fastcodecoq/6417480 to your computer and use it in GitHub Desktop.
Save fastcodecoq/6417480 to your computer and use it in GitHub Desktop.
Obtener la extensión de un archivo en Php
<?php
function get_extension ($file_name = "simple._práctico_.inc.txt"){
try{
if(!preg_match("/^([a-zA-Z0-9àèìòùáéíóúäëïöüÀÈÌÒÙÁÉÍÓÚÄËÏÖÜ\s\._-]+)$/i", $file_name))
return false;
else{
$file_name = explode(".", $file_name);
if(!count($extension) > 1)
{
return false;
}else{
$extension = (String) strtolower( end( $file_name));
return $extension;
}
}
} catch(Excetpion $e){
return false;
}
}
/* USO
get_extension($_FILES["name"]);
|| en caso de tratarse de carga múltiple
foreach($_FILES["name"] as $file_name) { get_extension($file_name); } (en caso de multiples archivos)
*/
//TEST
$ext = get_extension();
if($ext)
echo $ext;
else
echo "Parece que no has pasado un nombre de archivo válido";
//happy coding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment