Php 帮助将错误检查添加到此函数

Php 帮助将错误检查添加到此函数,php,gdlib,Php,Gdlib,我需要在此函数中添加一些错误检查,以确保imagegif和imagecolorset函数成功。失败的常见原因是权限问题,其中spot2.gif文件不可写 但是,当我将spot2.gif更改为只读时(这种情况应该触发else条件),echo的javascript警报不会触发 function set_theme_color_spot2($hex) { $info = hexToRGB($hex); $token = "../wp-content/themes/".get_optio

我需要在此函数中添加一些错误检查,以确保imagegif和imagecolorset函数成功。失败的常见原因是权限问题,其中spot2.gif文件不可写

但是,当我将spot2.gif更改为只读时(这种情况应该触发else条件),echo的javascript警报不会触发

function set_theme_color_spot2($hex)
{
    $info = hexToRGB($hex);
    $token = "../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2.gif";
    $token2 = "../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2-template.gif";
    if (file_exists($token2) && is_writable($token)) 
    {
        $img = imagecreatefromgif("../wp-content/themes/".get_option('template')."/styles/".get_option('myTheme')."/spot2-template.gif");
        $color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]);
        imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]);
        imagegif($img, $token);
        $themecolor = get_option('myTheme').'_color4';
        update_option($themecolor, $hex);
    }
    else
    {   
        echo "<script type='text/javascript'>alert('The template colors could not be changed because your current file permissions are too restrictive. Make sure the files in the styles directory are set to 644');</script>";
    }
}

在尝试访问spot2.gif之前,为什么不在spot2.gif上写入调用


如果您有一个有效的图像句柄,那么imagecolorset应该不会失败。它只是在某个地方的表格中设置一些值。另一方面,imagegif可以也将失败,因为它必须处理文件系统。不幸的是,它只返回true/false,而没有任何有用的诊断详细信息。如果知道它是否因为磁盘空间不足、写入文件的权限被拒绝、访问目录的权限被拒绝、没有这样的文件/目录等原因而失败,那将非常有用

所以,只要做:

if (!imagegif($handle, 'testfile.gif')) {
    die("Unable to write gif out");
}

使用您希望保存的任何诊断信息。您也可以尝试使用,但不能保证GD在出现故障时会填充任何有用的内容。

这对我来说非常有意义。我已经更新了我的问题,并在条件块中添加了可写检查。但是,即使spot2.gif被手动设置为只读,它仍然在执行……有什么想法吗?