在php中隐藏代码错误但仍然执行页面的其余部分

Ger*_*ira 2 php exception-handling exception

我希望有人可以帮助我:我有这样的脚本,如果我的服务器上不存在文件,它会转到远程服务器检查文件.如果文件存在,则将其复制到本地服务器,不再检查.因此,Imagick部分仅在我的本地服务器上不存在图像时才有效.

我遇到的问题是,如果远程服务器上不存在该文件 - 那么应用程序会出错 - 这是我脚本的代码:

        <?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(); }?>
Run Code Online (Sandbox Code Playgroud)

这是错误代码:

> 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
Run Code Online (Sandbox Code Playgroud)

如何避免此错误但仍能正常加载页面?

我试过了error_reporting(0); <---一旦发生错误,这将停止页面完全加载.

任何想法,将不胜感激.

我找到了所有答案的解决方案!太感谢了

<?php if(file_exists($filename))
Run Code Online (Sandbox Code Playgroud)

{echo''; } else {try {$ imageurlfolder = dirname($ filename); @mkdir($ imageurlfolder,0755,true); @copy($ imgremoteurl,$ filename); $ thumb = new Imagick($ filename); $ thumb-> scaleImage($ g_sites_img1,0); $拇指> writeImage($文件名); $ thumb-> destroy();} catch(ImagickException $ e){echo"Exception caught!\n"; }>?>

Dav*_*dža 5

好吧,你应该抓住异常,不要试图忽略错误.毕竟,您将遇到致命错误,这会阻止执行更多逻辑.

try
{
    // your logic
}
catch ( ImagickException $e )
{
    // do something with it
}
Run Code Online (Sandbox Code Playgroud)

  • 缺少E和c之间的'x' (2认同)