RSS Forums RSS

PHP file reading error

Thread Solved
Reply
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

PHP file reading error

  #1  
Feb 24th, 2008
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...
  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 5:40 pm. Reason: Please use code tags
Anything one man can make - another man will try to break.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Help Re: PHP file reading error

  #2  
Mar 13th, 2008
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:
  1. $pattern = "$dir/*.txt";
  2. $files = glob($pattern);
  3. var_dump($files);
  4. ...
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 736 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:45 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC