Monday 25 January 2016

Basic Error Handling: Using the die() function

Basic Error Handling: Using the die() function

The first example shows a simple script that opens a text file:
<?php
$file=fopen("welcome.txt","r");
?>
If the file does not exist you might get an error like this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in C:\webfolder\test.php on line 2
To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it:
<?php
if(!file_exists("welcome.txt")) {
  die("File not found");
} else {
  $file=fopen("welcome.txt","r");
}
?>
Now if the file does not exist you get an error like this:
File not found 
 

Related Articles

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive

nal waro

scarch

scarchpoint

Komentar

Artciles

News

Paling Dilihat