PHP as per path and filename in variables

Asked

Viewed 1,168 times

0

Hello. I have a table that should show the name of the files and the date they were updated, but nothing appears. In HTML I have only

<table><?php tabela("/arquivos/formularios/*.*"); ?></table> 

And in PHP:

function tabela($var){
    $globVar = glob($var);  

    for ($i=0; $i<count($globVar); $i++) {
        $caminho = $globVar[$i];
        if (file_exists($caminho)) {
            $nome = basename($caminho,".*");            
            echo "<tr><td class=\"td1\"><a href=\"".$caminho."\">".$nome."</a></td>
<td class=\"td2\">".date("d/m/Y", filemtime($caminho))."</td></tr>";
        }
    }   
}

When I load the page, I just see

<table></table>

Maybe I’m getting the glob() or other wrong function. I’d appreciate it if someone could help me.

  • Maybe the table input is wrong. Try arquivos/formularios/*.* relative path to script

  • That’s right, @gmsantos. Thank you!

  • 1

    Right after the line $globVar = glob($var);, try to display the values contained in the array using the print_r for example. Just to check if something is being returned from the function glob().

  • Hi @Adrianoleal. It is. The problem was the same way. I do not understand very well the difference between the front or not. I will look it up later. Thank you

  • At least in Linux environment / demonstrates that you are searching from the root of your file system, i.e., root. Without using the / you search from the current directory of your application.

  • It just keeps leaving the extension. basename() does not work with ".*"?

  • I know, I think we can make it explode()

Show 2 more comments

3 answers

1

why don’t you use scandir is much easier

example:

<?php

/*remove as pastas ".." e "." que podem aparecer em sistemas linux*/
$scan_dir = array_diff(scandir(__DIR__),array("..","."));

foreach ($scan_dir as $file_or_folder) {
    print $file_or_folder."<br/>";
}

?>

output

input.txt
main.php
folder1
folder2
  • I think it’s a good one too. VLW

1

Function tabela():

<?php

function tabela($var){
  error_reporting(E_ERROR | E_PARSE);
  $globVar = glob($var);
  $scan_dir = array_diff(scandir($var),array("..","."));
  echo "<table>";
  echo "<thead><th>Nome do Arquivo</th><th>Tamanho do Arquivo</th></thead>";
  foreach ($scan_dir as $file_or_folder) {
    print "<tr><td>".$file_or_folder."</td>";
    if (substr("$file_or_folder", 0, 1) != "."){
      print "<td>".date("d/m/Y", filemtime($file_or_folder))."</td></tr>";

    } else{
      print "</tr>";
    }
  }
  echo '</table>';
}
?>

Example of Use:

<?
tabela('./')
?>

Upshot:
inserir a descrição da imagem aqui

Implemented @William Borba’s suggestion, in the image instead of the size is the date, (detail that went unnoticed :))

  • Vlw @Ronny. When I took out "" at the beginning of the path, as gmsantos suggested, it worked. But your answers seem more elegant than my code. I’m not a programmer, I’m starting now, so I need to study scandir(). But if the staff decide q this is the best answer, I mark it. Abçs. Thank you. To you and to all!

0

  • Welcome to Stack Overflow! Try to better contextualize your question by exemplifying and explaining how to use this feature. Enjoy and make a [tour] to know how things work and read the guide to [Ask]

  • Reading suggestion: [Answer]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.