943,790 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1275
  • PHP RSS
Feb 24th, 2008
0

PHP file reading error

Expand Post »
Hi all,

Can anyone shed any light on this really simple problem please?

I need to read a list of files in a directory on a server. I am using PHP to do something like this...
PHP Syntax (Toggle Plain Text)
  1. $counter=0;
  2. if ($handle = opendir($dir)) {
  3. while (false !== ($file = readdir($handle))) {
  4. if ($file != "." && $file != "..") {
  5. $filetype=(pathinfo($file));
I then decide using an if(($filetype[extension]=="txt")||($filetype... to choose what to do with the file.

The problem is this. the readdir($handle) function does not return filenames if the filename has a blank space within its name. So it will read "myfile.txt" okay, but will skip "my file.txt" the problem seems to be the space in the filename because if I manually remove the spaces (from my test files) the function works perfectly well.

Oh and by the way, unfortunately I cannot guarantee file-name consistency in here, so I have to assume there will be names with spaces!

Any suggestions would be really appreciated. thanks in advance, JohnB.
Last edited by MattEvans; Feb 24th, 2008 at 6:40 pm. Reason: Please use code tags
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Mar 13th, 2008
0

Re: PHP file reading error

Hi John,
I'm reluctant to believe you on the spaces issue with readdir(). I checked the manual and nobody in the discussion has ever mentioned it. Maybe you think that it skips the files but in fact, something else down the road in your script does.

Anyways, try using glob() instead. It will make your script look prettier :-)
Something along these lines:
php Syntax (Toggle Plain Text)
  1. $pattern = "$dir/*.txt";
  2. $files = glob($pattern);
  3. var_dump($files);
  4. ...
Reputation Points: 27
Solved Threads: 16
Junior Poster
petr.pavel is offline Offline
116 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: open_basedir restriction in effect error in PHP
Next Thread in PHP Forum Timeline: php implementation of Boyer-Moor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC