As returns only the file name without the path

Asked

Viewed 1,031 times

2

How do I get this code to return only the file name.

With this current code it’s returning me all the way from the ex file:

/IES.pptx files

I need him to just return the name or the name with Ex extension:

IES or IES.pptx

Follows the code.

<?php
// Recupera a lista de todos os arquivos:
$itens = glob("arquivos/*");

// Ordena os arquivos pela data de modificacão:
usort($itens, function ($a, $b) { return filemtime($a) < filemtime($b); });

// Pega apenas os cinco últimos modificados:
$cont = array_slice($itens, 0, 5);

foreach ($cont   as $arq )

echo  "<a href=  ".$arq." > ".$arq." </a><br>";
?>

3 answers

2


A basename should only solve:

<?php
// Recupera a lista de todos os arquivos:
$itens = glob("arquivos/*");

// Ordena os arquivos pela data de modificacão:
usort($itens, function ($a, $b) {
   return filemtime($a) < filemtime($b);
});

// Pega apenas os cinco últimos modificados:
$cont = array_slice($itens, 0, 5);

foreach ($cont as $arq) {
    $nome = basename($arq);
    echo '<a href="' . $arq . '"> '. $nome . '</a><br>';
}

Or (depends on what you want):

$nome = basename($arq);
echo '<a href="' . $nome . '"> '. $nome . '</a><br>';

If you want without the extension (if the files are all of the same format):

<?php
// Recupera a lista de todos os arquivos:
$itens = glob("arquivos/*");

// Ordena os arquivos pela data de modificacão:
usort($itens, function ($a, $b) {
   return filemtime($a) < filemtime($b);
});

// Pega apenas os cinco últimos modificados:
$cont = array_slice($itens, 0, 5);

foreach ($cont as $arq) {
    $nome = basename($arq, '.pptx');
    echo '<a href="' . $arq . '"> '. $nome . '</a><br>';
}

If it has various extensions:

<?php
// Recupera a lista de todos os arquivos:
$itens = glob("arquivos/*");

// Ordena os arquivos pela data de modificacão:
usort($itens, function ($a, $b) {
   return filemtime($a) < filemtime($b);
});

// Pega apenas os cinco últimos modificados:
$cont = array_slice($itens, 0, 5);

foreach ($cont as $arq) {
    $nome = pathinfo($arq, PATHINFO_FILENAME);
    echo '<a href="' . $arq . '"> '. $nome . '</a><br>';
}

0

Can match strrchr() which returns the last part of a string after the delimiter (it enters together as return) and trim() to remove the / remainder.

$arr = array('public/html/p1/js/f.js', 'p2/main/file.txt', 'p3/readme.txt', 'pics/1.jpg');

foreach ($arr as $item) {
    echo trim(strrchr($item, '/'), '/') .'<br>';
}

Exit:

f.js
file.txt
readme.txt
1.jpg

Another option is to use function pathinfo(), the second argument tells her to return only the file name. The folder, extension and the name without extension are other options.

foreach ($arr as $item) {
    echo  pathinfo($item, PATHINFO_BASENAME) .'<br>';
}

0

To get the file name from the full path:

 $this_page = basename($_SERVER['REQUEST_URI']); // caminho e nome do arquivo
 if (strpos($this_page, "?") !== false) $this_page = reset(explode("?", $this_page));
  • 2

    Young man, where did you get this? The question talks about file system, not url. I denied your answer, because it doesn’t make sense here.

  • $_SERVER['REQUEST_URI'] returns the path of php running and hitchhiked. The rest of the stretch basename() and the if is what settles the issue. Alias, basename() is at the basis of the accepted answer. Next, I will be more explicit... Or maybe not.

  • The problem is that it has nothing to do with what was asked

Browser other questions tagged

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