Jump to content

list directory contents into $list=


swamp

Recommended Posts

With the help of some of the guys on this forum I've got this code:

 

<?php
$list  = "file 1/file 2/file 3";
$pieces = explode("/", $list); 
?>

<? echo $pieces[0]; ?>

<? echo $pieces[1]; ?>

 

<?php
foreach(glob('../../author/*.txt') as $file){
     $file = substr($file, strrpos($file, '/') + 1); //get the position of the last / and remove what's before it
     $file = substr($file, 0, strrpos($file, '.')); //get the position of the last dot and remove what's after it
     echo $file;
} 
?>

 

but I want to have it so that the $list = all the files in the directory with a / after it...

 

so if the directory had file20, file21, file 22 etc...

 

the $list would = file20/file21/file22

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/120681-list-directory-contents-into-list/
Share on other sites

Use something like this:

 

<?php
$list = '';
foreach(glob('../../author/*.txt') as $file){
     $file = pathinfo($file, PATHINFO_BASENAME); //another guy suggested you this method so use it instead of substr()
     $list = $file . '/';
}
$list = rtrim($list, '/'); //remove the last backslash
$pieces = explode('/', $list);
?>

 

If there isn't any special reason you are first assigning all the files to a string and then explode it to an array, you can initially pass all those values to an array:

<?php
$list = array();
foreach(glob('../../author/*.txt') as $file){
     $file = pathinfo($file, PATHINFO_BASENAME);
     $list[] = $file; //add the file as an array element
}
foreach($list as $val){
     echo $val;
}
?>

Ok cool - I've got this almost working now:

 

<?php
$list = '';
foreach(glob('../author/*.txt') as $file){
     $file = substr($file, strrpos($file, '/') + 1); //get the position of the last / and remove what's before it
     $file = substr($file, 0, strrpos($file, '.')); //get the position of the last dot and remove what's after it
 $list = $file . '/';
}
$list = rtrim($list, '/'); //remove the last backslash
$pieces = explode('/', $list);
?>


<strong><? echo $pieces[0]; ?></strong><br />
<strong><? echo $pieces[1]; ?></strong><br />
<strong><? echo $pieces[2]; ?></strong><br />

 

but its only displaying the first one ($pieces[0]) not [1], [2], etc...

 

Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.