[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/UI/WebControls/ -> TFileUpload.php (source)

   1  <?php
   2  /**
   3   * TFileUpload class file
   4   *
   5   * @author Marcus Nyeholt <tanus@users.sourceforge.net>, Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TFileUpload.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Web.UI.WebControls
  11   */
  12  
  13  /**
  14   * TFileUpload class
  15   *
  16   * TFileUpload displays a file upload field on a page. Upon postback,
  17   * the text entered into the field will be treated as the name of the file
  18   * that will be uploaded to the server. The property {@link getHasFile HasFile}
  19   * indicates whether the file upload is successful. If successful, the file
  20   * may be obtained by calling {@link saveAs} to save it at a specified place.
  21   * You can use {@link getFileName FileName}, {@link getFileType FileType},
  22   * {@link getFileSize FileSize} to get the original client-side file name,
  23   * the file mime type, and the file size information. If the upload is not
  24   * successful, {@link getErrorCode ErrorCode} contains the error code
  25   * describing the cause of failure.
  26   *
  27   * TFileUpload raises {@link onFileUpload OnFileUpload} event if a file is uploaded
  28   * (whether it succeeds or not).
  29   *
  30   * @author Marcus Nyeholt <tanus@users.sourceforge.net>, Qiang Xue <qiang.xue@gmail.com>
  31   * @version $Id: TFileUpload.php 1397 2006-09-07 07:55:53Z wei $
  32   * @package System.Web.UI.WebControls
  33   * @since 3.0
  34   */
  35  class TFileUpload extends TWebControl implements IPostBackDataHandler, IValidatable
  36  {
  37      /**
  38       * Maximum file size (in bytes) allowed to be uploaded, defaults to 1MB.
  39       */
  40      const MAX_FILE_SIZE=1048576;
  41      /**
  42       * @var integer the size of the uploaded file (in bytes)
  43       */
  44      private $_fileSize=0;
  45      /**
  46       * @var string The original name of the file on the client machine
  47       */
  48      private $_fileName='';
  49      /**
  50       * @var string the name of the temporary file storing the uploaded file
  51       */
  52      private $_localName='';
  53      /**
  54       * @var string the uploaded file mime type
  55       */
  56      private $_fileType='';
  57      /**
  58       * @var integer error code of the current file upload
  59       */
  60      private $_errorCode=UPLOAD_ERR_NO_FILE;
  61  
  62      /**
  63       * @return string tag name of the file upload control
  64       */
  65  	protected function getTagName()
  66      {
  67          return 'input';
  68      }
  69  
  70      /**
  71       * Sets name attribute to the unique ID of the control.
  72       * This method overrides the parent implementation with additional file update control specific attributes.
  73       * @param THtmlWriter the writer used for the rendering purpose
  74       */
  75  	protected function addAttributesToRender($writer)
  76      {
  77          $this->getPage()->ensureRenderInForm($this);
  78          parent::addAttributesToRender($writer);
  79          $writer->addAttribute('type','file');
  80          $writer->addAttribute('name',$this->getUniqueID());
  81      }
  82  
  83      /**
  84       * Sets Enctype of the form on the page.
  85       * This method overrides the parent implementation and is invoked before render.
  86       * @param mixed event parameter
  87       */
  88  	public function onPreRender($param)
  89      {
  90          parent::onPreRender($param);
  91          if(($form=$this->getPage()->getForm())!==null)
  92              $form->setEnctype('multipart/form-data');
  93          $this->getPage()->getClientScript()->registerHiddenField('MAX_FILE_SIZE',$this->getMaxFileSize());
  94          if($this->getEnabled(true))
  95              $this->getPage()->registerRequiresPostData($this);
  96      }
  97  
  98      /**
  99       * @return integer the maximum file size, defaults to 1MB (1048576 bytes).
 100       * @see setMaxFileSize
 101       */
 102  	public function getMaxFileSize()
 103      {
 104          return $this->getViewState('MaxFileSize',self::MAX_FILE_SIZE);
 105      }
 106  
 107      /**
 108       * Sets the maximum size that a file can be uploaded.
 109       * Note, this is an advisory value to the browser. Sets this property with
 110       * a reasonably large size to save users the trouble of waiting
 111       * for a big file being transferred only to find that it was too big
 112       * and the transfer failed.
 113       * @param int the maximum upload size allowed for a file.
 114       */
 115  	public function setMaxFileSize($size)
 116      {
 117          $this->setViewState('MaxFileSize',TPropertyValue::ensureInteger($size),self::MAX_FILE_SIZE);
 118      }
 119  
 120      /**
 121       * @return string the original full path name of the file on the client machine
 122       */
 123  	public function getFileName()
 124      {
 125          return $this->_fileName;
 126      }
 127  
 128      /**
 129       * @return integer the actual size of the uploaded file in bytes
 130       */
 131  	public function getFileSize()
 132      {
 133          return $this->_fileSize;
 134      }
 135  
 136      /**
 137       * @return string the MIME-type of the uploaded file (such as "image/gif").
 138       * This mime type is not checked on the server side and do not take its value for granted.
 139       */
 140  	public function getFileType()
 141      {
 142          return $this->_fileType;
 143      }
 144  
 145      /**
 146       * @return string the local name of the file (where it is after being uploaded).
 147       * Note, PHP will delete this file automatically after finishing this round of request.
 148       */
 149  	public function getLocalName()
 150      {
 151          return $this->_localName;
 152      }
 153  
 154      /**
 155       * Returns an error code describing the status of this file uploading.
 156       * @return integer the error code
 157       * @see http://www.php.net/manual/en/features.file-upload.errors.php
 158       */
 159  	public function getErrorCode()
 160      {
 161          return $this->_errorCode;
 162      }
 163  
 164      /**
 165       * @return boolean whether the file is uploaded successfully
 166       */
 167  	public function getHasFile()
 168      {
 169          return $this->_errorCode===UPLOAD_ERR_OK;
 170      }
 171  
 172      /**
 173       * Saves the uploaded file.
 174       * @param string the file name used to save the uploaded file
 175       * @param boolean whether to delete the temporary file after saving.
 176       * If true, you will not be able to save the uploaded file again.
 177       * @return boolean true if the file saving is successful
 178       */
 179  	public function saveAs($fileName,$deleteTempFile=true)
 180      {
 181          if($this->_errorCode===UPLOAD_ERR_OK)
 182          {
 183              if($deleteTempFile)
 184                  return move_uploaded_file($this->_localName,$fileName);
 185              else if(is_uploaded_file($this->_localName))
 186                  return file_put_contents($fileName,file_get_contents($this->_localName))!==false;
 187              else
 188                  return false;
 189          }
 190          else
 191              return false;
 192      }
 193  
 194      /**
 195       * Loads user input data.
 196       * This method is primarly used by framework developers.
 197       * @param string the key that can be used to retrieve data from the input data collection
 198       * @param array the input data collection
 199       * @return boolean whether the data of the control has been changed
 200       */
 201  	public function loadPostData($key,$values)
 202      {
 203          if(isset($_FILES[$key]))
 204          {
 205              $this->_fileName=$_FILES[$key]['name'];
 206              $this->_fileSize=$_FILES[$key]['size'];
 207              $this->_fileType=$_FILES[$key]['type'];
 208              $this->_errorCode=$_FILES[$key]['error'];
 209              $this->_localName=$_FILES[$key]['tmp_name'];
 210              return true;
 211          }
 212          else
 213              return false;
 214      }
 215  
 216      /**
 217       * Raises postdata changed event.
 218       * This method calls {@link onFileUpload} method.
 219       * This method is primarly used by framework developers.
 220       */
 221  	public function raisePostDataChangedEvent()
 222      {
 223          $this->onFileUpload(null);
 224      }
 225  
 226      /**
 227       * This method is invoked when a file is uploaded during a postback.
 228       * The method raises <b>OnFileUpload</b> event to fire up the event handler.
 229       * If you override this method, be sure to call the parent implementation
 230       * so that the event delegates can be invoked.
 231       * @param TEventParameter event parameter to be passed to the event handlers
 232       */
 233  	public function onFileUpload($param)
 234      {
 235          $this->raiseEvent('OnFileUpload',$this,$param);
 236      }
 237  
 238      /**
 239       * Returns the original file name as the property value to be validated.
 240       * This method is required by IValidatable property.
 241       * @return mixed the property value to be validated
 242       */
 243  	public function getValidationPropertyValue()
 244      {
 245          return $this->getFileName();
 246      }
 247  }
 248  
 249  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7