/** * Rename existing file. * * @param string $source * @param string $destination * @return string * / public function rename($source, $destination) { if (file_exists($source)) { $dir = dirname($destination); if (! is_dir($dir)) { $oldmask = umask(0777); if (! @mkdir($dir, 0777, true)) { $this->throwLastError("Could not create '$dir' directory."); } umask($oldmask); } if (file_exists($destination)) { if (! unlink($destination)) { $this->throwLastError("Could not remove existing '$destination' file."); } } if (! rename($source, $destination)) { $this->throwLastError("Could not rename '$source' to '$destination'."); } } else { $this->throwLastError("Source file '$source' does not exist."); } } */ protected function throwLastError($msg) { if ($last = \MUtil_Error::getLastPhpErrorMessage()) { $msg .= sprintf($this->translate->_(' The error message is: %s'), $last); } throw new \Gems_Exception_Coding($msg); }
/** * Ensure the directory does really exist or throw an exception othewise * * @param string $dir The path of the directory * @param int $mode Unix file mask mode, ignored on Windows * @return string the directory * @throws \Zend_Exception */ public static function ensureDir($dir, $mode = 0777) { // Clean up directory name $dir = self::cleanupSlashes($dir); if (!is_dir($dir)) { if (!@mkdir($dir, $mode, true)) { throw new \Zend_Exception(sprintf("Could not create '%s' directory: %s.", $dir, \MUtil_Error::getLastPhpErrorMessage('reason unknown'))); } } return $dir; }
/** * Save a single model item. * * @param array $newValues The values to store for a single model item. * @param array $filter If the filter contains old key values these are used * to decide on update versus insert. * @return array The values as they are after saving (they may change). */ public function save(array $newValues, array $filter = null) { $filename = false; if ($this->recursive) { if (isset($newValues['relpath'])) { $filename = $newValues['relpath']; } } if (!$filename && isset($newValues['filename'])) { $filename = $newValues['filename']; } if (!$filename) { throw new \MUtil_Model_ModelException('Cannot save file: no filename known'); } $filename = trim($filename, '\\/'); if ($this->dir) { $filename = $this->dir . DIRECTORY_SEPARATOR . $filename; } $content = isset($newValues['content']) ? $newValues['content'] : ''; \MUtil_File::ensureDir(dirname($filename)); if (false === file_put_contents($filename, $content) || !file_exists($filename)) { throw new \MUtil_Model_ModelException(sprintf('Unable to save %s: %s', $filename, \MUtil_Error::getLastPhpErrorMessage('reason unknown'))); } $this->setChanged(1); return $newValues; }