如何从输入文件中获取扩展名?

时间:2015-11-27 20:03:36

标签: php

我的问题是这个,我将文件上传到我的服务器,但是我想在所选文件的末尾添加日期和扩展名(重写),我使用的是爆炸功能,但我不知道得到任何结果或字符串。

以下是代码:

  <?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>"; 
}
?>

提前致谢:)

2 个答案:

答案 0 :(得分:1)

你可以通过这种方式获取文件extesion

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

TXT

答案 1 :(得分:1)

如果阻止,则无需在中使用explode和许多条件。您只需执行此操作即可获得文件扩展名:

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