CFile::set PHP Method

set() public method

Essentially path supplied by user is resolved into real path (see {@link getRealPath}), all the other property getting methods should use that real path. Uploaded files are supported through {@link CUploadedFile} Yii class. Path aliases are supported through {@link getPathOfAlias} Yii method.
public set ( string $filepath, boolean $greedy = False ) : object
$filepath string Path to the file specified by user, if not set exception is raised
$greedy boolean If True file properties (such as 'Size', 'Owner', 'Permission', etc.) would be autoloaded
return object CFile instance for the specified filesystem object
    public function set($filepath, $greedy = False)
    {
        if (trim($filepath) != '') {
            $uploaded = null;
            if (strpos($filepath, '\\') === False && strpos($filepath, '/') === False) {
                $uploaded = CUploadedFile::getInstanceByName($filepath);
                if ($uploaded) {
                    $filepath = $uploaded->getTempName();
                    $this->addLog('File "' . $filepath . '" is identified as uploaded', 'trace');
                } elseif ($aliased_path = $this->getPathOfAlias($filepath)) {
                    $this->addLog('The string supplied to set() - "' . $filepath . '" is identified as the alias to "' . $aliased_path . '"', 'trace');
                    $filepath = $aliased_path;
                }
            }
            clearstatcache();
            $cl = get_class($this);
            $realPath = $cl::realPath($filepath);
            $inst = $cl::getInstance($realPath);
            $inst->_filepath = $filepath;
            $inst->_realpath = $realPath;
            if ($inst->exists()) {
                $inst->_uploaded_inst = $uploaded;
                $inst->pathInfo();
                $inst->readable;
                $inst->writeable;
                if ($greedy) {
                    $inst->isempty;
                    $inst->size;
                    $inst->owner;
                    $inst->group;
                    $inst->permissions;
                    $inst->timeModified;
                    if ($inst->isFile) {
                        $inst->mimeType;
                    }
                }
            }
            return $inst;
        }
        throw new CFileException('Path to filesystem object is not specified within set() method');
    }

Usage Example

示例#1
0
 /**
  * Helper method. Return an appropriate CFile instance for a given filesystem object path.
  *
  * @static
  * @param string $filepath Path to the file.
  * @param bool $greedy If `True` file properties (such as 'Size', 'Owner', 'Permission', etc.) would be autoloaded
  * @return CFile
  */
 public static function get($filepath, $greedy = false)
 {
     if (self::$_obj === null) {
         self::$_obj = new self();
     }
     return self::$_obj->set($filepath, $greedy);
 }