コード例 #1
0
ファイル: file.php プロジェクト: sd-studio/or
 /**
    @param $field  name of send field - key in $_FILES array
    @param $path   store uploaded file to
    @param $name   new name of file (without extension) or keep original if empty
    @param $allowedTypes  check file type to be one of these or skip if empty array
    @param $maxSizeBytes  skip if =0
 
    @return  full path to stored file or null if an error occured
 */
 public static function upload($field, $path, $name = null, $allowedTypes = array(), $maxSizeBytes = 0)
 {
     if (!array_key_exists($field, $_FILES)) {
         return null;
     }
     $params = $_FILES[$field];
     $fnm = $params['name'];
     $err = $params['error'];
     if ($err != UPLOAD_ERR_OK) {
         if ($err == UPLOAD_ERR_NO_FILE) {
             return null;
         } else {
             if ($err == UPLOAD_ERR_INI_SIZE) {
                 throw new Exception("File upload error: file size exceeds 'upload_max_filesize' parameter set in php.ini");
             }
         }
         throw new Exception("File upload error: error code [{$err}]");
     }
     if (!is_uploaded_file($params['tmp_name'])) {
         throw new Exception('File upload error: file was not actually uploaded');
     }
     if ($maxSizeBytes && $params['size'] > $maxSizeBytes) {
         throw new Exception("File upload error: file [{$fnm}] exceeds maximum size");
     }
     $ftype = strtolower(substr(strrchr($params['name'], '.'), 1));
     if (!empty($allowedTypes) && !in_array($ftype, $allowedTypes)) {
         throw new Exception("File upload error: invalid file type [{$ftype}]");
     }
     $fnm = $name ? "{$name}.{$ftype}" : $fnm;
     $savePath = String::append($path, '/') . $fnm;
     if (!move_uploaded_file($params['tmp_name'], $savePath)) {
         throw new Exception('File upload error: can not move uploaded file');
     }
     chmod($savePath, 0644);
     return $savePath;
 }
コード例 #2
0
ファイル: String.php プロジェクト: reoring/sabel
 public function testAppend()
 {
     $string = new String("hoge");
     $this->assertTrue($string->append("hoge")->equals("hogehoge"));
     $hoge = new String("hoge");
     $huga = new String("huga");
     $this->assertTrue($hoge->append($huga)->equals("hogehuga"));
 }