2010-10-14 25 views
0

我需要添加一些錯誤檢查功能以確保imagegif()和imagecolorset()函數成功。通常的失敗原因是spot2.gif文件不可寫的權限問題。幫助將錯誤檢查添加到此功能

但是,當我將spot2.gif更改爲只讀,應觸發「其他」條件的條件時,echo'd 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>"; 
    } 
} 

回答

0

在嘗試訪問它之前,爲什麼不在你的spot2.gif上調用「is_writable」?

http://php.net/manual/de/function.is-writable.php

+0

這對我來說非常有意義。我已經更新了我的問題,並在條件塊中添加了is_writable檢查。然而,它仍然在執行,即使當spot2.gif被手動設置爲只讀時...任何想法? – 2010-10-14 16:47:59

0

如果你有一個有效的圖像處理,然後imagecolorset()應該不能夠失敗。它只是在某個表中設置一些值。另一方面,imagegif()可能並會失敗,因爲它必須處理文件系統。不幸的是,它只返回真/假,而不是任何有用的診斷細節(如果因爲磁盤空間不足而失敗,拒絕寫入文件的權限,拒絕訪問目錄的權限,沒有這樣的文件/目錄等...)。

所以,只是做:

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

與任何診斷信息,你會在乎曾經擁有保存。你也可以嘗試使用error_get_last(),但是不能保證GD會在失敗時使用任何有用的東西。