By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

error handling issue - try / catch with PHP 5

P: n/a
I think I'm not quite understanding something about error handling in
PHP5. I have written some PHP code to index the contents of C drive
on a Windows machine. When it gets to certain special folders (fake
folders) it runs into errors. Directories it can not read from. I
have tried wrapping the code in a Try / Catch and the error still
comes through. What am I missing?

<?php
// Here is the code:
function indexFiles($path, $handle) {
try {
$d = dir($path);
while (false !== ($entry = $d->read())) {
if (is_dir($path . '\\' . $entry)) {
if (($entry != ".") && ($entry != "..")) {
indexFiles($path . '\\' . $entry, $handle);
}
} else {
fwrite($handle, $path . '\\' . $entry . "\n");
}
}
$d->close();
} catch (Exception $e) { }
}

$p = "C:";
$fp = fopen('M:\\data.txt', 'w+t');
indexFiles($p, $fp);
fclose($fp);
?>

I get the error specifically when the script runs into the "junction
point" C:\Documents and Settings.
Junction point is similar to a symlink but on Windows Vista

Warning: dir(C:\Documents and Settings): failed to open dir: No error
in C:\test.php on line 4
Fatal error: Call to a member function read() on a non-object in C:
\test.php on line 5

Does try / catch not work? Any good articles on what I am seeing?

Thanks,
Kelly Greer
ke*********@nospam.com
change nospam to yahoo
Sep 12 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
kellygreer1 wrote:
I think I'm not quite understanding something about error handling in
PHP5. I have written some PHP code to index the contents of C drive
on a Windows machine. When it gets to certain special folders (fake
folders) it runs into errors. Directories it can not read from. I
have tried wrapping the code in a Try / Catch and the error still
comes through. What am I missing?

<?php
// Here is the code:
function indexFiles($path, $handle) {
try {
$d = dir($path);
while (false !== ($entry = $d->read())) {
if (is_dir($path . '\\' . $entry)) {
if (($entry != ".") && ($entry != "..")) {
indexFiles($path . '\\' . $entry, $handle);
}
} else {
fwrite($handle, $path . '\\' . $entry . "\n");
}
}
$d->close();
} catch (Exception $e) { }
}

$p = "C:";
$fp = fopen('M:\\data.txt', 'w+t');
indexFiles($p, $fp);
fclose($fp);
?>

I get the error specifically when the script runs into the "junction
point" C:\Documents and Settings.
Junction point is similar to a symlink but on Windows Vista

Warning: dir(C:\Documents and Settings): failed to open dir: No error
in C:\test.php on line 4
Fatal error: Call to a member function read() on a non-object in C:
\test.php on line 5

Does try / catch not work? Any good articles on what I am seeing?

Thanks,
Kelly Greer
ke*********@nospam.com
change nospam to yahoo
No, errors in PHP code do not trigger try/catch blocks. Some classes
now do that, but the dir() function does not. You need to check the
result for false to see if it worked.

The first question I would have here - does the user running the script
(i.e. the web server if you're running under one) have authorization to
access this "junction point"? If so, I would suspect it's a(nother)
problem with running things on Vista.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #2

P: n/a
..oO(kellygreer1)
>I think I'm not quite understanding something about error handling in
PHP5. I have written some PHP code to index the contents of C drive
on a Windows machine. When it gets to certain special folders (fake
folders) it runs into errors. Directories it can not read from. I
have tried wrapping the code in a Try / Catch and the error still
comes through. What am I missing?
You could try to define your own error handler to turn PHP errors and
warnings into exceptions.

http://www.php.net/manual/en/functio...or-handler.php

But there are still some errors that can't be catched.

Micha
Sep 12 '08 #3

P: n/a
On Sep 12, 3:26*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(kellygreer1)
I think I'm not quite understanding something about error handling in
PHP5. *I have written some PHP code to index the contents of C drive
on a Windows machine. When it gets to certain special folders (fake
folders) it runs into errors. Directories it can not read from. *I
have tried wrapping the code in a Try / Catch and the error still
comes through. *What am I missing?

You could try to define your own error handler to turn PHP errors and
warnings into exceptions.

http://www.php.net/manual/en/functio...or-handler.php

But there are still some errors that can't be catched.

Micha
Thanks I'll have to look at setting the error handler.
I tried the other idea... checking the result of the dir() function.
I'm testing the script on WinXP machine now. And even with testing the
result of dir() I still get an error when the script hits the "C:
\System Volume Information"

Warning: dir(C:\\System Volume Information): failed to open dir:
Result too large in C:\crawl.php on line 6

Fatal error: Call to a member function read() on a non-object in C:
\crawl.php on line 8

Any ideas on trapping the weird dir() error / warning issue? how was
this solved back in the PHP 4 days?
Going to look at this custom handler now.
Thanks,
Kelly
Sep 12 '08 #4

P: n/a
On Sep 12, 3:40*pm, kellygreer1 <kellygre...@yahoo.comwrote:
On Sep 12, 3:26*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(kellygreer1)
>I think I'm not quite understanding something about error handling in
>PHP5. *I have written some PHP code to index the contents of C drive
>on a Windows machine. When it gets to certain special folders (fake
>folders) it runs into errors. Directories it can not read from. *I
>have tried wrapping the code in a Try / Catch and the error still
>comes through. *What am I missing?
You could try to define your own error handler to turn PHP errors and
warnings into exceptions.
http://www.php.net/manual/en/functio...or-handler.php
But there are still some errors that can't be catched.
Micha

Thanks I'll have to look at setting the error handler.
I tried the other idea... checking the result of the dir() function.
I'm testing the script on WinXP machine now. And even with testing the
result of dir() I still get an error when the script hits the "C:
\System Volume Information"

Warning: dir(C:\\System Volume Information): failed to open dir:
Result too large in C:\crawl.php on line 6

Fatal error: Call to a member function read() on a non-object in C:
\crawl.php on line 8

Any ideas on trapping the weird dir() error / warning issue? how was
this solved back in the PHP 4 days?
Going to look at this custom handler now.
Thanks,
Kelly
Ok, I was able to suppress error. By making the test in the same line
as the function ala:
if (false !== $d = dir("C:\\test")) {
echo $d;
} else {
echo "there was a crazy error";
}

But still get the warning... hmmm will read up more on Warnings.

Kelly
Sep 12 '08 #5

P: n/a
kellygreer1 wrote:
On Sep 12, 3:40 pm, kellygreer1 <kellygre...@yahoo.comwrote:
>On Sep 12, 3:26 pm, Michael Fesser <neti...@gmx.dewrote:
>>.oO(kellygreer1)
I think I'm not quite understanding something about error handling in
PHP5. I have written some PHP code to index the contents of C drive
on a Windows machine. When it gets to certain special folders (fake
folders) it runs into errors. Directories it can not read from. I
have tried wrapping the code in a Try / Catch and the error still
comes through. What am I missing?
You could try to define your own error handler to turn PHP errors and
warnings into exceptions.
http://www.php.net/manual/en/functio...or-handler.php
But there are still some errors that can't be catched.
Micha
Thanks I'll have to look at setting the error handler.
I tried the other idea... checking the result of the dir() function.
I'm testing the script on WinXP machine now. And even with testing the
result of dir() I still get an error when the script hits the "C:
\System Volume Information"

Warning: dir(C:\\System Volume Information): failed to open dir:
Result too large in C:\crawl.php on line 6

Fatal error: Call to a member function read() on a non-object in C:
\crawl.php on line 8

Any ideas on trapping the weird dir() error / warning issue? how was
this solved back in the PHP 4 days?
Going to look at this custom handler now.
Thanks,
Kelly

Ok, I was able to suppress error. By making the test in the same line
as the function ala:
if (false !== $d = dir("C:\\test")) {
echo $d;
} else {
echo "there was a crazy error";
}

But still get the warning... hmmm will read up more on Warnings.

Kelly
Alternatively - use opendir(), readdir() and closedir(). A little more
work, but not too bad.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Sep 12 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.