472,351 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 software developers and data experts.

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
ke*********@nospam.com
change nospam to yahoo
Sep 12 '08 #1
5 14171
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
..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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the...
14
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if...
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I...
9
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it...
1
by: GS | last post by:
Any points of what would be the good error handling design for application? User error handling in Application_OnError and throw() new errors on...
13
by: Ivan Weiss | last post by:
Good morning all, I am trying to access an access database to authenticate users upon logging into my application. Something is throwing an...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and...
2
by: Carol | last post by:
Exception may be thrown in the code inside the try block. I want to handling the SqlException with State == 1 in a special way, and for all others...
2
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.