Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PHP

error handling issue - try / catch with PHP 5

 
Jump to:  
 
kellygreer1
PostPosted: Fri Sep 12, 2008 3:14 pm    Post subject: error handling issue - try / catch with PHP 5
       
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
kellygreer1@nospam.com
change nospam to yahoo
 

 
Jerry Stuckle
PostPosted: Fri Sep 12, 2008 3:14 pm    Post subject: Re: error handling issue - try / catch with PHP 5
       
kellygreer1 wrote:
Quote:
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
kellygreer1@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.
jstucklex@attglobal.net
==================
 

 
Michael Fesser
PostPosted: Fri Sep 12, 2008 5:26 pm    Post subject: Re: error handling issue - try / catch with PHP 5
       
..oO(kellygreer1)

Quote:
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.

LINK

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

Micha
 

 
Jerry Stuckle
PostPosted: Fri Sep 12, 2008 6:47 pm    Post subject: Re: error handling issue - try / catch with PHP 5
       
kellygreer1 wrote:
Quote:
On Sep 12, 3:40 pm, kellygreer1 <kellygre...@yahoo.com> wrote:
On Sep 12, 3:26 pm, Michael Fesser <neti...@gmx.de> wrote:



.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.
LINK
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.
jstucklex@attglobal.net
==================
 

 
kellygreer1
PostPosted: Fri Sep 12, 2008 7:40 pm    Post subject: Re: error handling issue - try / catch with PHP 5
       
On Sep 12, 3:26 pm, Michael Fesser <neti...@gmx.de> wrote:
Quote:
.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.

LINK

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
 

 
kellygreer1
PostPosted: Fri Sep 12, 2008 7:58 pm    Post subject: Re: error handling issue - try / catch with PHP 5
       
On Sep 12, 3:40 pm, kellygreer1 <kellygre...@yahoo.com> wrote:
Quote:
On Sep 12, 3:26 pm, Michael Fesser <neti...@gmx.de> wrote:



.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.

LINK

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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Armatura Łazienki koszulki Szyba poker Teka