1

I looked on the forums but did not find what I need. I need help to delete the extension from the file name, and include all files for example (php files) from a directory, and show you examples.

This code work

$name = 'file.php';    
$fileName= pathinfo($name, PATHINFO_FILENAME );

echo "Name: {$fileName}";

Results:

Name: file

but how do I get this with several files included in the folder

<?php          
$dir = "/dir1/dir2/dir3/dir4/";
$phpfiles = glob($dir . "*.php");

foreach ($phpfiles as $phpfile){

     echo '<li><a href="'.$phpfile.'">'.basename($phpfile,".php").'</a></li>'; 
}
output
1.php
2.php
3.php
4.php
-----------------
how to insert path info in this script so that all files are included without extensions.
?>
user2988099
  • 1
  • 1
  • 7
  • 1
    Your code works well for me, I would only add additional dot to the second argument - `basename($phpfile,".php")` – zavg May 22 '14 at 09:21
  • if you need to modify the array `$phpfile` instead of doing it when you are displaying it you can use `$phpfiles=array_map(function($f){return basename($f, ".php");},$phpfiles);` – bansi May 22 '14 at 09:30
  • Bansi, do not quite understand what to do with your code that you have written if he can better explanation. – user2988099 May 22 '14 at 09:34

2 Answers2

1

By using pathinfo function...??

<?php          
$dir = "/dir1/dir2/dir3/dir4/";
$phpfiles = glob($dir . "*.php");

foreach ($phpfiles as $phpfile){
     echo '<li><a href="'.$phpfile.'">'.pathinfo($phpfile, PATHINFO_FILENAME).'</a></li>'; 
}
RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
0

If you want to display the filenames only you can use @zavg's suggestion (just add a .). If you want to actually modify the array for later use you can use array_map.

$dir = "/dir1/dir2/dir3/dir4/";
$phpfiles = glob($dir . "*.php");
//using basename
$phpfiles=array_map(function($f){return basename($f, ".php");},$phpfiles);
//Or Using pathinfo
//$phpfiles=array_map(function($f){return pathinfo($f, PATHINFO_FILENAME );},$phpfiles);
foreach ($phpfiles as $phpfile){
     echo '<li><a href="'.$dir.$phpfile.'">'.$phpfile.'</a></li>'; 
}
bansi
  • 55,591
  • 6
  • 41
  • 52
  • what is the error you are getting? what is the version of php you are using? – bansi May 22 '14 at 10:01
  • Bansi your code is not a good representative of the error in the `code lines $dir = "/dir1/dir2/dir3/dir4/"; $phpfiles = glob($dir . "*.php"); //using basename error line----$phpfiles=array_map(function($f){return basename($f, ".php");},$phpfiles); //Or Using pathinfo //error line---$phpfiles=array_map(function($f){return pathinfo($f, PATHINFO_FILENAME );},$phpfiles); foreach ($phpfiles as $phpfile){ echo '
  • '.$phpfile.'
  • '; }` – user2988099 May 22 '14 at 10:23
  • [Anonymous functions](http://www.php.net/manual/en/functions.anonymous.php) where introduced in PHP 5.3. if you are using older version of PHP this code won't work. Sorry I currently don't have older version of PHP to check this. – bansi May 22 '14 at 10:27
  • Using PHP Version 5.5.9, error line---$phpfiles=array_map(function($f){return pathinfo($f, PATHINFO_FILENAME );},$phpfiles); ,error line----$phpfiles=array_map(function($f){return basename($f, ".php");},$phpfiles); – user2988099 May 22 '14 at 10:44
  • I tested again with `5.4.16` and not getting any error. can you post the exact error message. – bansi May 22 '14 at 10:54
  • `Adobe dreamweaver shows syntax error, browser not shows syntax error links not not full path, coming out in the browser's sitename/file, and it should be sitename/dir1/dir2/dir3/dir4/file` – user2988099 May 22 '14 at 11:21
  • I have changed the code to include the full `$dir` also now it should be `/dir1/dir2/dir3/dir4/file` . Don't trust dremviewer with PHP code. – bansi May 22 '14 at 11:55
  • `when you put a slash at the beginning of dir variables, invisible links` – user2988099 May 22 '14 at 12:04