Reputation: 2941
I would like to open a file in PHP and the first thing I do is a check to see if it exists. Because of this I'm adding it to a a try catch block, so the script does not collapse. If the file does not exist the script should stop.
The code below is giving an error message the file could not be opened
.
(The file does not exist, for testing reasons)
try
{
$file_handle = fopen("uploads/".$filename."","r");
}
catch (Exception $hi)
{
die("Fehler");
}
This is the error displayed in my browser:
Warning: fopen(uploads/Testdatensatz_Bewerbungenn.csv): failed to open stream: No such file or directory in [...]\bewerbungToDB.php on line 11
Upvotes: 10
Views: 5442
Reputation: 219874
That's not an exception. It's a PHP warning. Try/catch is for catching exceptions only. If you want to "catch" that error you should check the value of $file_handle
and if it is false throw an exception.
try
{
$file_handle = @fopen("uploads/".$filename."","r");
if (!$file_handle) {
throw new Exception('Failed to open uploaded file');
}
}
catch (Exception $hi)
{
die("Fehler");
}
Upvotes: 19