473,503 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14301
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
4372
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 variable is defined as private at form-level. So...
14
3861
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 the file selected is too big. I do know about the...
4
7600
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 have the following function in a class module...
9
2212
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 does what I want, but I'm still not convinced that...
1
1864
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 conditions through the code? I'd like utlimiately to...
13
1618
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 excecption. My error handling code should display...
35
3736
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 difficult to maintain, when the number of error checks...
2
2181
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 I want to use a general way to handle. Which of...
2
1863
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 errors in a program some time can make errors...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.