/** * gets a file pointer from a specified file * @param string $filename * @param array $options * @return mixed $res file pointer | true | false */ public static function getFPFromFile($filename, $options = array()) { if (isset($options)) { self::$options = $options; } if (!file_exists($filename)) { self::$errors[] = lang::translate('File does not exists') . ' : ' . $options['filename']; return false; } if (isset($options['maxsize'])) { $size = filesize($options['filename']); // check the file is less than the maximum file size if ($size > $options['maxsize']) { $error = lang::translate('File is too large.'); $error .= lang::translate('Max size is ') . bytes::bytesToGreek($options['maxsize']); error_log($error); self::$errors[] = $error; return false; } } // check for right content if (isset($options['allow_mime'])) { $type = file::getMime($options['filename']); if (!in_array($type, $options['allow_mime'])) { self::$errors[] = lang::translate('This Content type is not allowed') . MENU_SUB_SEPARATOR_SEC . $type; return false; } } $fp = fopen($filename, 'rb'); return $fp; }
/** * checks if if size is allowed * @param string $filename the name of the file * @param int $maxsize bytes * @return boolean $res */ public static function checkMaxSize($file, $maxsize = null) { if (!$maxsize) { $maxsize = self::$options['maxsize']; } if ($file['size'] > $maxsize) { $error = lang::translate('File is too large.') . ' '; $error .= lang::translate('Max size is ') . ' ' . bytes::bytesToGreek($maxsize); log::error($error); self::$errors[] = $error; return false; } return true; }
/** * sets a file field with info about max file size * @param string $filename * @param int $max_bytes * @param array $options */ public static function fileWithLabel($filename, $max_bytes, $options = array()) { $bytes = bytes::getNativeMaxUpload(); if ($max_bytes < $bytes) { $bytes = $max_bytes; } self::hidden('MAX_FILE_SIZE', $bytes); $label = lang::translate('Max size per file') . ". "; $size = bytes::bytesToGreek($bytes); $label .= $size; if (isset($options['multiple'])) { $max_files = ini_get('max_file_uploads'); $label .= "<br />"; $label .= lang::translate('Max number of files: <span class="notranslate">{MAX_FILES}</span>', array('MAX_FILES' => $max_files)); } else { $label .= "<br />"; $label .= lang::translate('Only one file at a time'); } if (isset($options['allowed'])) { $label .= '<br />'; $label .= lang::translate('Allowed file-types: ') . $options['allowed']; } self::label($filename, $label); self::file($filename, $options); }