473,890 Members | 1,227 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($pat h, $handle) {
try {
$d = dir($path);
while (false !== ($entry = $d->read())) {
if (is_dir($path . '\\' . $entry)) {
if (($entry != ".") && ($entry != "..")) {
indexFiles($pat h . '\\' . $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:\Document s 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*********@nos pam.com
change nospam to yahoo
Sep 12 '08 #1
5 14333
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($pat h, $handle) {
try {
$d = dir($path);
while (false !== ($entry = $d->read())) {
if (is_dir($path . '\\' . $entry)) {
if (($entry != ".") && ($entry != "..")) {
indexFiles($pat h . '\\' . $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:\Document s 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*********@nos pam.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*******@attgl obal.net
=============== ===

Sep 12 '08 #2
..oO(kellygreer 1)
>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.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.

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...@ya hoo.comwrote:
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.
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...@ya hoo.comwrote:
>On Sep 12, 3:26 pm, Michael Fesser <neti...@gmx.de wrote:
>>.oO(kellygree r1)
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*******@attgl obal.net
=============== ===

Sep 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4442
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 if "global variables get re-set" doesn't tell the whole story, then what does? ***please note*** I'm not looking for a solution - I'm looking for a more detailed description of what happens when an un-handled error occurs - possibly with help file...
14
3906
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 maxRequestLength parameter of the <httpRuntime> section and that works as expected. What I want is to enforce a max file size but haven't been able to trap the error thrown when the file is too large and that's where I could use some help.
4
7622
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 (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader ' Declare the SQL data layer class Dim oSQL As New...
9
2236
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 I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
1
1875
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 consolidate all error_handling in one method which I'll be able to easily modify to write to event log or text file etc instead of error hanlding scattered through the code. Thanks, GS
13
1651
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 this message in a msgbox. However, the messagebox is always empty. Here is my error handling code.
35
3818
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 gets about 3 or so. It also gets very awkward in nested loops, where you want to check for normal loop processing in the loop condition, not errors. Yes, you could put some generic exit flag in the loop condition, but when you're simply done if...
2
2208
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 the following options is better? ----------------------------------------------------- Option 1: try{...} catch (System.Data.SqlClient.SqlException ex) { if (ex.State == 1)
2
1889
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 occur! Project details: 1- New solution for handling errors in .net 2- Try...Catch...Finally Block 3- Error handling sample
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9820
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10461
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8015
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3276
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.