PHP file reading error
Please support our PHP advertiser: Free Online Load Test - How many visitors can your web site handle?
Thread Solved
![]() |
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...
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.
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)
$counter=0; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filetype=(pathinfo($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.
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:
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)
$pattern = "$dir/*.txt"; $files = glob($pattern); var_dump($files); ...
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.
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.
![]() |
Similar Threads
Other Threads in the PHP Forum
- Setting up a form in PHP (PHP)
- Open In New Window Php (PHP)
- Parse error, unexpected T_STRING, expecting T_CASE (PHP)
- PHP5x,Apache2.2 : Apache not reading php file (PHP)
- find the cause of the error in the C prog (C)
- Invalid Syntax Error (Viruses, Spyware and other Nasties)
- "DNS Error Page" (Viruses, Spyware and other Nasties)
- Error Message Concerning Reading File From A Drive (C++)
- error msg BRIDGE DLL newbie needs help (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: open_basedir restriction in effect error in PHP
- Next Thread: php implementation of Boyer-Moor
•
•
•
•
Views: 736 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode