| | |
PHP file reading error
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
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 6: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
- 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
| Thread Tools | Search this Thread |
# 5.2.10 action address apache api array auto autoincrement beginner binary broken cakephp checkbox class classes cms code cron curl database date dehasher destroy display dissertation domain dynamic echo echo$_get[x]changingitintovariable... email error errorlog fatalerror file files folder form forms function functions google href htaccess html if-else image images include insert ip javascript joomla legislation limit link load login mail masterthesis menu mlm multiple mysql mysqlquery oop open paypal pdf persist php popup problem query radio random record recursion remote script search server sessions sms sockets source space sql syntax system table tutorial update upload url validator variable video web youtube





