PHP How to get the name of a variable file?

1

I am trying to import an excel book in PHP but the name of this file is changed every day.

$nombreArchivo="/Carpeta/archivovariable.xlsx"

Is there any code to add in the path such as "%%" or "*" so that the file is imported without importing the name?

I appreciate your help, I found the solution and here I leave it. This code reads all the files within a directory and gets its name and extension:

<?php
$directorio = opendir("."); //ruta actual
while ($archivo = readdir($directorio)) //obtenemos un archivo y luego otro sucesivamente
{
    if (is_dir($archivo))//verificamos si es o no un directorio
    {
         //Aqui hacen algo si no es un directorio
    }
    else
    {
        echo $archivo . "<br />"; //Aqui hacen lo que quieran con cada      archivo
    }
}
?>

Source: link

    
asked by Spirit 30.01.2018 в 23:40
source

1 answer

2

You can use opendir and readdir or scandir or glob . Assuming you know the base directory.

$directorio_base = __DIR__ ;

$dir_handle = opendir($directorio_base);

while(($archivo = readdir($dir_handle)) !== false) {
  $ruta = $directorio_base . '/' . $archivo;
  echo $ruta . PHP_EOL;
  if(is_file($ruta)) {
      $ext = pathinfo($ruta, PATHINFO_EXTENSION);
      if($ext === 'xlsx') {
          //hacer lo que se tiene que hacer con el archivo
      }
  }
}
closedir($dir_handle);

With glob

$directorio_base = __DIR__ ;

$archivos = glob($directorio_base . '/*.xlsx');

foreach($archivos as $archivo) {
    //trabajar con cada archivo
}
    
answered by 30.01.2018 в 23:58