dxcoder1
dxcoder1

Reputation: 299

loop through the files in a folder in php

i have searched through the Internet and found the scrip to do this but am having some problems to read the file names.

here is the code

$dir = "folder/*";
 foreach(glob($dir) as $file)  
 {  
 echo $file.'</br>';  
}

this display in this format

folder/s0101.htm
folder/s0692.htm

for some reasons i want to get them in this form.

s0101.htm
s0692.htm

can anyone tell me how to do this?

Upvotes: 14

Views: 12808

Answers (2)

Just use basename() wrapped around the $file variable.

<?php
$dir = "folder/*";
foreach(glob($dir) as $file)
{
    if(!is_dir($file)) { echo basename($file)."\n";}
}

The above code ignores the directories and only gets you the filenames.

Upvotes: 15

Girish
Girish

Reputation: 12117

You can use pathinfo function to get file name from dir path

$dir = "folder/*";
 foreach(glob($dir) as $file) {  
  $pathinfo = pathinfo($file);
  echo $pathinfo['filename']; // as well as other data in array print_r($pathinfo);
}

Upvotes: 5

Related Questions