Problem when listing files in a directory, the first result shows blank

2

I do not know the reason why when listing the files in the directory, the first result is always printed in white, as shown in the attached image.

The code I am using is:

<?php
// Lista los temas alfabeticamente
$ruta = "archivos/songs/";
$archi = "";
$filehandle = opendir($ruta);
while (false !== ($file = readdir($filehandle))) {
if ($file != "." && $file != ".." && substr($file,-4)==".mp3") {     
    $arch=$file;
    $archi=$arch.'*'.$archi;  
  }
}
closedir($filehandle);

// Inicio paginación 
$archivosfile = explode ("*", $archi);
sort($archivosfile);
$currentpage = $_SERVER['PHP_SELF']; // Página donde se encuentra 
$total=(count($archivosfile)-1); 
$maxRows = 10; // Cantidad máxima de archivos a mostrar por página 
$pageNum = 0; // Página de inicio

if (isset($_GET['pag'])) { 
$pageNum = intval($_GET['pag']); 
} 
$startRow = $pageNum * $maxRows; 

if (isset($_GET['pag'])) { 
$pageNum = intval($_GET['pag']); 
} 
$startRow = $pageNum * $maxRows; 
$totalRows = $total; 
$totalPages = ceil($totalRows/$maxRows)-1; 
$archivos = array_slice($archivosfile, $startRow, $maxRows); 
// Fín paginación    
?>

<div class="container">
  <div class="row">
    <div style="float:left;padding:4px;margin-left:1px;">
    <h2>Los <?php echo $total;  ?> temas de la lista</h2>
    </div>
  </div>
</div>

<?php
foreach ($archivos as $archivo) {
$archivo = preg_replace("#.mp3#", '', $archivo); 
echo '<div class="caja-menu seccion" style="height: 185px">
<img src="archivos/songs/'.$archivo.'.jpg" onerror="imgError(this);" style="float:left;margin:16px" width="125" height="125"> 
<p><p>
<a href=""><span style="font-size:180%">'.$archivo.'</span></a>
<br>
<a class="btn btn-default" role="button" target="_self" href="indexSongsPlayer.php?id=archivos/songs/'.$archivo.'.mp3">
<span class="glyphicon glyphicon-headphones"></span>&nbsp;&nbsp;Escuchar</a>';
echo '</div>';
}
?>
    
asked by Jaime Vinuesa 25.01.2018 в 09:31
source

1 answer

4

The problem is that for each element you are always adding a * .

Suppose you have the following files: Abba1 and Abba2 . When you add the name of the file to the variable $archi , you do this: $archi=$arch.'*'.$archi; , that is:

// En la primer vuelta
$archi = 'Aba1*';
// En la segunda vuelta
$archi = 'Aba2*Aba1*';

If you pay attention, you have 2 * , then when doing $archivosfile = explode ("*", $archi); , it will result in a Array with 3 elements and the last one will be empty. Finally when doing sort($archivosfile); that element will be first in the array.

Solutions:

Option 1

You could use substr to remove the last * of String

//... código anterior
$archi = substr($archi, 0, -1);
$archivosfile = explode ("*", $archi);

Option 2

You could directly define a Array and add the clues in this.

$ruta = "archivos/songs/";
$archivosfile = []; // Eliminamos la variable '$archi'
$filehandle = opendir($ruta);
while (false !== ($file = readdir($filehandle))) {
if ($file != "." && $file != ".." && substr($file,-4)==".mp3") {     
    $archivosfile[]=$file; // Agregamos las pistas al arreglo
  }
}
closedir($filehandle);

// Inicio paginación 
sort($archivosfile); // Ordenamos el arreglo
//... resto del código
    
answered by 25.01.2018 / 11:14
source