1

I need to check the files inside a folder contains '_' symbol. I have used the glob function to get the files from server location. But I am unaware to check the filenames contains the symbol anywhere within the filename. I have files having names like the following format. student_178_grade1A

I have done like this.

$report_files=glob( '/user_uploads/'  . '/reportcards/' . 'term' . '_' . 10.'/*'.'.pdf' );

//this will return all files inside the folder.

if(count(report_files)>0)
 {
        //some stuff
 }
 else
 {

 }

I need to get the files which has '_' within the filename.I tried

glob( '/user_uploads/'  . '/reportcards/' . 'term' . '_' . 10.'/*[_]'.'.pdf' );

but its not working

Techy
  • 2,626
  • 7
  • 41
  • 88

2 Answers2

3

Your regular expression doesn't seem correct. This might do what you want:

// Find any file in the directory "/user_uploads/reportcards/term_10/" 
// that has the file extension ".pdf"
$report_files = glob("\/user_uploads\/reportcards\/term\_10\/(.*)\.pdf");

// Find any file in the directory "/user_uploads/reportcards/term_10/" 
// containing the character "_".
$report_files = glob("\/user_uploads\/reportcards\/term\_10\/(.*)\_(.*)");

// Find any file in the directory "/user_uploads/reportcards/term_10/" 
// that has the file extension ".pdf" and contains the "_" character
$report_files = glob("\/user_uploads\/reportcards\/term\_10\/(.*)\_(.*)\.pdf");

If you don't understand exactly what the regular expression does, I made a quick summary of what is being done down below. There's also a great website to try out regular expresison with documentation on how to construct them here.

\/ = escapes the / character
\_ = escapes the _ character
\. = escapes the . character
(.*) = matches any character, number etc
Paradoxis
  • 4,471
  • 7
  • 32
  • 66
  • 1
    I have used $report_files = glob("\/user_uploads\/reportcards\/term\_10'.'/*'.'_*\.pdf'"); //this works for me. Thanks for giving me the idea. – Techy Jun 29 '15 at 06:52
1

First off you forgot a quotation mark after term.

$report_files = glob('/user_uploads/'.'/reportcards/'.'term'.'_'.10.'/*[_]'.'.pdf');

Secondly you have two slashes after user_uploads.

$report_files = glob('/user_uploads/reportcards/'.'term'.'_'.10.'/*[_]'.'.pdf');
Trevi Awater
  • 2,387
  • 2
  • 31
  • 53