/** * Constructor * * Instantiate a directory object * * @param string $dir * @param boolean $full * @param boolean $rec * @throws Exception * @return void */ public function __construct($dir, $full = false, $rec = false) { // Check to see if the directory exists. if (!file_exists(dirname($dir))) { $lang = new Moc10_Language(); throw new Exception($lang->__('Error: The directory does not exist.')); } else { $this->_full = $full; $this->_rec = $rec; // Set the directory path. if (strpos($dir, '/') !== false && DIRECTORY_SEPARATOR != '/') { $this->path = str_replace('/', "\\", $dir); } else { if (strpos($dir, "\\") !== false && DIRECTORY_SEPARATOR != "\\") { $this->path = str_replace("\\", '/', $dir); } else { $this->path = $dir; } } // Trim the trailing slash. if (strrpos($this->path, DIRECTORY_SEPARATOR) == strlen($this->path) - 1) { $this->path = substr($this->path, 0, -1); } // If the recursive flag is passed, traverse recursively. if ($this->_rec) { $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path), RecursiveIteratorIterator::SELF_FIRST); foreach ($objects as $fileInfo) { if ($fileInfo->getFilename() != '.' && $fileInfo->getFilename() != '..') { // If full path flag was passed, store the full path. if ($this->_full) { $this->files[] = $fileInfo->isDir() ? $fileInfo->getPathname() . DIRECTORY_SEPARATOR : $fileInfo->getPathname(); // Else, store only the directory or file name. } else { $this->files[] = $fileInfo->isDir() ? $fileInfo->getFilename() . DIRECTORY_SEPARATOR : $fileInfo->getFilename(); } } } // Else, only traverse the single directory that was passed. } else { foreach (new DirectoryIterator($this->path) as $fileInfo) { if (!$fileInfo->isDot()) { // If full path flag was passed, store the full path. if ($this->_full) { $this->files[] = $fileInfo->isDir() ? $this->path . DIRECTORY_SEPARATOR . $fileInfo->getFilename() . DIRECTORY_SEPARATOR : $this->path . DIRECTORY_SEPARATOR . $fileInfo->getFilename(); // Else, store only the directory or file name. } else { $this->files[] = $fileInfo->isDir() ? $fileInfo->getFilename() . DIRECTORY_SEPARATOR : $fileInfo->getFilename(); } } } } } }
/** * Constructor * * Instantiate a file object based on a file that has been uploaded. * * @param string $upload * @param string $file * @param int $size * @param array $types * @throws Exception * @return void */ public function __construct($upload, $file, $size = null, $types = null) { $lang = new Moc10_Language(); // Check to see if the upload directory exists. if (!file_exists(dirname($file))) { throw new Exception($lang->__('Error: The upload directory does not exist.')); } // Check to see if the permissions are set correctly. if ($this->_checkPermissions(dirname($file)) != 777) { throw new Exception($lang->__('Error: Permission denied.')); } // Move the uploaded file, creating a file object with it. if (move_uploaded_file($upload, $file)) { chmod($file, 0777); parent::__construct($file, true, $types); // Check the file size requirement. if (!is_null($size) && $this->size > $size) { $this->delete(); throw new Exception($lang->__('Error: The file uploaded is too big.')); } else { if (is_null($size) && !is_null($this->_max) && $this->size > $this->_max) { $this->delete(); throw new Exception($lang->__('Error: The file uploaded is too big.')); } } } else { throw new Exception($lang->__('Error: There was an error in uploading the file.')); } }
/** * Check file or directory permissions. * * @param string $file * @throws Exception * @return string */ protected function _checkPermissions($file) { $perm = ''; if (DIRECTORY_SEPARATOR == '/') { $perm = substr(sprintf('%o', fileperms($file)), -3); } else { if (!is_writable($file)) { throw new Exception($this->_lang->__('Error: The file or directory (%1) is not writable.', $file)); } else { $perm = 777; } } return $perm; }