Reputation: 1337
I am hoping that someone can help me: I have this scripts that if a file does not exists on my server, it goes to a remote server to check for the file. If the file exists it copies it to my local server, and does not check it again. So the Imagick part only works when the image does not exists on my local server.
The problem that I have is that if the file does not exists on the remote server - then the application throughs a error - Here is the code of my script:
<?php if (file_exists($filename))
{
echo '<img src="'.$imageurl1.'" width="'.$g_sites_img1.'" alt="'.$imageurlmeta.'" class="image1" align="left" />';
}
else { $imageurlfolder = dirname($filename);
@mkdir($imageurlfolder, 0755, true);
@copy($imgremoteurl, $filename);
$thumb = new Imagick($filename);
$thumb->scaleImage($g_sites_img1, 0);
$thumb->writeImage($filename);
$thumb->destroy(); }?>
Here is the error code:
> Fatal error: Uncaught exception
> 'ImagickException' with message
> 'Unable to read the file:
> /home/game1/public_html/images/small///.jpg'
> in
> /home/game1/public_html/includes/standard__1.php:15
> Stack trace: #0
> /home/game1/public_html/includes/standard__1.php(15):
> Imagick->__construct('/home/game1/pub...')
> #1 /home/game1/public_html/includes/news.php(127):
> require('/home/game1/pub...') #2
> /home/game1/public_html/index1.php(126):
> include('/home/game1/pub...') #3
> {main} thrown in
> /home/game1/public_html/includes/standard__1.php
> on line 15
How can I avoid this error but still make the page load normally?
I have tried error_reporting(0); <--- This stops the page from completely loading once the error has occured.
Any ideas would be appreciated.
I have found the solution with all the answers posted! thanks a million
<?php if(file_exists($filename))
{ echo ''; } else { try {$imageurlfolder = dirname($filename); @mkdir($imageurlfolder, 0755, true); @copy($imgremoteurl, $filename); $thumb = new Imagick($filename); $thumb->scaleImage($g_sites_img1, 0); $thumb->writeImage($filename); $thumb->destroy();} catch (ImagickException $e) { echo "Exception caught!\n"; } } ?>
Upvotes: 2
Views: 3722
Reputation: 7207
Well, you should catch the exception, don't try ignoring the errors. After all, you are getting fatal error which prevents further logic from being executed.
try
{
// your logic
}
catch ( ImagickException $e )
{
// do something with it
}
Upvotes: 5
Reputation: 36619
Fatal errors are just that -- FATAL. The script has died, and it's not going to go any farther because the rest of it depends on that data being present.
Add some error-handling in the event that the file doesn't exist on the remote server, and you should be ok.
Upvotes: -3
Reputation: 2616
Yes, what you are looking for is called a try-catch statement. Here is what PHP docs have to say about it
Upvotes: 1
Reputation: 12037
This is an exception, not an error.
You need to catch it and then handle it:
try {
$thumb = new Imagick($filename);
// do your thing with it
$thumb->destroy();
} catch (ImagickException $e) {
// something went wrong, handle the problem
}
Upvotes: 1
Reputation: 7505
use a catch block
http://php.net/manual/en/internals2.opcodes.catch.php
Upvotes: 1
Reputation: 2941
You can use a default local image if the file does not exists on the remote server.
Upvotes: 1
Reputation: 224903
Enclose it in a try
block and you can handle the error and choose to ignore it or whatever you want to do with it.
Upvotes: 0
Reputation: 522081
if (copy($imgremoteurl, $filename)) {
// image functions
} else {
// error copying image
}
Upvotes: 0
Reputation: 3261
error_reporting(0); works for errors, not exceptions:
try {
// your code
} catch (Exception $e) {
// code that runs in case an error appears
}
Upvotes: 0