PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/class/util/UploadedFile.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 171 lines | 101 code | 16 blank | 54 comment | 3 complexity | 8a14e99d21ea9a22dcbc608db06e703e MD5 | raw file
  1. <?php
  2. /**
  3. * アップロードされたファイル情報を保持するクラス
  4. *
  5. * PHP version 5
  6. *
  7. * @package class.util
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 stk2k, sazysoft
  10. */
  11. class Charcoal_UploadedFile extends Charcoal_File
  12. {
  13. private $name;
  14. private $type;
  15. private $size;
  16. private $tmp_name;
  17. private $error;
  18. /*
  19. * コンストラクタ
  20. *
  21. * @param String userfile $_FILES変数のキー
  22. */
  23. public function __construct( $userfile )
  24. {
  25. $file = $_FILES[ us($userfile) ];
  26. $this->name = $file['name'];
  27. $this->type = $file['type'];
  28. $this->size = $file['size'];
  29. $this->tmp_name = $file['tmp_name'];
  30. $this->error = $file['error'];
  31. parent::__construct( $this->tmp_name );
  32. }
  33. /*
  34. * クライアントマシンの元のファイル名
  35. *
  36. */
  37. public function getOriginalName()
  38. {
  39. return $this->name;
  40. }
  41. /*
  42. * ファイルの MIME 型
  43. *
  44. */
  45. public function getType()
  46. {
  47. return $this->type;
  48. }
  49. /*
  50. * ファイルのバイト単位のサイズ
  51. *
  52. */
  53. public function getSize()
  54. {
  55. return $this->size;
  56. }
  57. /*
  58. * テンポラリファイルの名前を取得
  59. *
  60. */
  61. public function getTmpName()
  62. {
  63. return $this->tmp_name;
  64. }
  65. /*
  66. * テンポラリファイルを取得
  67. *
  68. */
  69. public function getTmpFile()
  70. {
  71. return new Charcoal_File( $this->tmp_name );
  72. }
  73. /*
  74. * テンポラリファイルのコンテンツを取得
  75. *
  76. */
  77. public function getTmpFileContents()
  78. {
  79. if ( !file_exists($this->tmpname) || !is_readable($this->tmpname) ){
  80. return NULL;
  81. }
  82. return file_get_contents( $this->tmpname );
  83. }
  84. /*
  85. * エラーコード
  86. *
  87. */
  88. public function getError()
  89. {
  90. return $this->error;
  91. }
  92. /*
  93. * Fileオブジェクトに変換
  94. *
  95. */
  96. public function toFile()
  97. {
  98. return new Charcoal_File( s($this->tmp_name) );
  99. }
  100. /*
  101. * オリジナルファイル名の拡張子を取得
  102. *
  103. */
  104. public function getExtension()
  105. {
  106. $pos = strrpos( $this->name, '.' );
  107. if ( is_int($pos) ){
  108. return substr( $this->name, $pos );
  109. }
  110. return '';
  111. }
  112. /*
  113. * エラーチェック
  114. *
  115. */
  116. public function checkErrors()
  117. {
  118. switch( $this->error ){
  119. case UPLOAD_ERR_OK:
  120. log_info( "system,debug", "UPLOAD_ERR_OK" );
  121. break;
  122. case UPLOAD_ERR_INI_SIZE:
  123. log_info( "system,debug", "UPLOAD_ERR_INI_SIZE" );
  124. _throw( new Charcoal_FileUploadIniSizeException($this) );
  125. break;
  126. case UPLOAD_ERR_FORM_SIZE:
  127. log_info( "system,debug", "UPLOAD_ERR_FORM_SIZE" );
  128. _throw( new Charcoal_FileUploadFormSizeException($this) );
  129. break;
  130. case UPLOAD_ERR_PARTIAL:
  131. log_info( "system,debug", "UPLOAD_ERR_PARTIAL" );
  132. _throw( new Charcoal_FileUploadPartialException($this) );
  133. break;
  134. case UPLOAD_ERR_NO_FILE:
  135. log_info( "system,debug", "UPLOAD_ERR_NO_FILE" );
  136. _throw( new Charcoal_FileUploadNoFileException($this) );
  137. break;
  138. case UPLOAD_ERR_NO_TMP_DIR:
  139. log_info( "system,debug", "UPLOAD_ERR_NO_TMP_DIR" );
  140. _throw( new Charcoal_FileUploadNoTmpDirException($this) );
  141. break;
  142. case UPLOAD_ERR_CANT_WRITE:
  143. log_info( "system,debug", "UPLOAD_ERR_CANT_WRITE" );
  144. _throw( new Charcoal_FileUploadCantWriteException($this) );
  145. break;
  146. case UPLOAD_ERR_EXTENSION:
  147. log_info( "system,debug", "UPLOAD_ERR_EXTENSION" );
  148. _throw( new Charcoal_FileUploadExtensionException($this) );
  149. break;
  150. default:
  151. log_warning( "system,debug", "unexpected upload error:" . $this->error );
  152. break;
  153. }
  154. }
  155. }