Bitrix Site Manager

CFile::IsImage

string
CFile::IsImage(
 string file_name,
 mixed mime_type = false
)

The method IsImage checks the file extension and MIME type. Returns true if the file extension and type corresponds to an image file, or false otherwise.

Parameters

ParameterDescription
file_name File name (without path).
mime_type MIME type (e.g. "image/").

Optional. The default value is "false", which disables checks for MIME type.

See Also

Example



<?
if ($rsFiles = CTicket::GetFileList($v1="s_id", 
                                    $v2="asc", 
                                    array("HASH" => $hash))) :
  if ($arFile = $rsFiles->Fetch()) :
    $filename = $_SERVER["DOCUMENT_ROOT"]."/".
                COption::GetOptionString("main", 
                                         "upload_dir", 
                                         "upload")."/".
                $arFile["SUBDIR"]."/".$arFile["FILE_NAME"];
    if ($f = fopen($filename, "rb"))
    {
      $is_image = CFile::IsImage($arFile["FILE_NAME"], 
                                 $arFile["CONTENT_TYPE"]);
      // if image
      if ($is_image) 
      {
        // send as image
        header("Content-type: ".$arFile["CONTENT_TYPE"]);
        header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0"); 
        header("Expires: 0"); 
        header("Pragma: public"); 
        while ($buffer = fread($f, 4096)) echo $buffer;
      }
      else
      {
        // send as text
        header("Content-type: text/html; charset=".LANG_CHARSET);
        header("Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0"); 
        header("Expires: 0"); 
        header("Pragma: public"); 
        echo "<pre>";
        while ($buffer = fread($f, 4096)) 
            echo htmlspecialchars($buffer);
        echo "</pre>";
      }
      fclose ($f);
      die();
    }
  endif;
endif;
?>