Neobugu
Neobugu

Reputation: 333

How to obtain extension from input file?

My issue is this, I am uploading files to my server, but I want to add the date and the extension, at the end of the selected file (re-write), I am using explode function but I don'y get any result or string.

Here is the code:

  <?php
$tism = "svr";
if(isset($_GET['Nombre'])){
$iso = $_GET['Nombre'];
$uno = mssql_query("SELECT *  FROM tbl_doc WHERE Nombre = '$iso' AND Carpeta = '$tism'");
$dos = mssql_fetch_array($uno);
unlink($dos["Ruta"]);
mssql_query("DELETE FROM tbl_doc WHERE Nombre = '$iso' AND Carpeta = '$tism'");


}
?>
<script>
function eliminar(nombre){
confirm("Esta seguro de eliminar este documento?");
document.location.href="desclist.php?Nombre="+nombre;
}
</script>
<br />
<center>
<form name="subir" method="POST" action="" enctype="multipart/form-data">
<table border="1">
<tr><input type="hidden" name="tism"  value="<?php echo $tism; ?>"/></tr>
<tr><td><label>Nombre del archivo: </label></td><td><input type="text" name="file_name" /></td></tr>
<tr><td><label for="file">Sube un archivo:</label></td>
<td><input type="file" name="archivo" id="archivo" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><input type="submit" name="boton" value="Subir" /></td>
</tr>
</table>
</form>
<div class="resultado"></div>
</center>
<br />


<?php

$sur = date("Ymdhis");
$target_dir ="../imagenes_fichas";
$nombre_archivo = $_POST['file_name'];
$otro = $_FILES["archivo"]["name"];
if(isset($_POST['boton'])){
$ext = explode(".",$_POST['archivo']);
if ((
($_FILES["archivo"]["type"] == "application/msword") || //doc
($_FILES["archivo"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) || //docx
($_FILES["archivo"]["type"] == "application/pdf") || //pdf
($_FILES["archivo"]["type"] == "application/vnd.ms-excel") || //xls
($_FILES["archivo"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") || //xlsx
($_FILES["archivo"]["type"] == "application/vnd.ms-powerpoint") || //ppt
($_FILES["archivo"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation") //pptx
) {

  if ($_FILES["archivo"]["error"] > 0) {
    echo $_FILES["archivo"]["error"] . "";
  } else {
    if (file_exists("$target_dir/$tism/" . $_FILES["archivo"]["name"].$sur.$ext[1])) {

      echo $_FILES["archivo"]["name"] . " ya existe. ";
    } else {
      move_uploaded_file($_FILES["archivo"]["tmp_name"],
      "$target_dir/$tism/" . $_FILES["archivo"]["name"].$sur.$ext[1]);
      mssql_query("INSERT INTO tbl_doc_desc VALUES ('$nombre_archivo','$target_dir/$tism/".$_FILES['archivo']['name']."$sur.".$ext[0]."','$tism')") or die (mssql_get_last_message());
      echo "<center>Archivo Subido<br/> </center>";
      }
  }
} else {
    echo "Archivo no permitido";
}
}
?>

<?php

$query = mssql_query("SELECT * FROM tbl_doc WHERE CARPETA ='$tism'");
if ($row = mssql_fetch_array($query)){
echo "<center><table border = '1' style='text-align: center;width:400px;' > \n";
echo "<tr><td>ID</td><td>Nombre</td><td>Ruta</td><td>Carpeta</td><td>Eliminar</td></tr>";
do {
    echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td><td><input type='button' onclick='eliminar(\"$row[1]\")' value='eliminar'/></td></tr>";
    } while ($row = mssql_fetch_array($query));
    echo "</table></center> \n"; 
} else { 
echo "<center>¡ No se ha encontrado ningún registro !</center>"; 
}
?>

Thanks in advance :)

Upvotes: 0

Views: 67

Answers (2)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

There's no need to use explode and so many conditions in if block. You can simply do this to get the file extension:

$ext = strtolower(pathinfo($otro, PATHINFO_EXTENSION));

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133410

you can get the file extesion this way

$info = new SplFileInfo('foo.txt');
var_dump($info->getExtension());

txt

Upvotes: 1

Related Questions