swamp Posted August 21, 2008 Share Posted August 21, 2008 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 More sharing options...
Fadion Posted August 21, 2008 Share Posted August 21, 2008 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; } ?> Link to comment https://forums.phpfreaks.com/topic/120681-list-directory-contents-into-list/#findComment-621863 Share on other sites More sharing options...
swamp Posted August 21, 2008 Author Share Posted August 21, 2008 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? Link to comment https://forums.phpfreaks.com/topic/120681-list-directory-contents-into-list/#findComment-622113 Share on other sites More sharing options...
swamp Posted August 21, 2008 Author Share Posted August 21, 2008 no worries- just had a curly bracket in the wrong place! Cheers Link to comment https://forums.phpfreaks.com/topic/120681-list-directory-contents-into-list/#findComment-622124 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.