/** * Creates an empty file with a unique name in the default directory for temporary files or in a specified * directory, and returns the path to the created file. * * After creating the file, the method sets the file's access permissions as "644", which means read/write access * for the file's owner (the user on whose behalf the application runs) and read-only access for other users. * * @param string $inDirectoryPath **OPTIONAL. Default is** *the default directory for temporary files*. The path * to the directory where the file is to be created. If this parameter is omitted, the used directory is either the * OS's default directory for temporary files or, if specified, the directory read from the application's * configuration. If specified, the path is not required to end with "/". * @param string $namePrefix **OPTIONAL. Default is** "tmp-". The name prefix for the file. * * @return CUStringObject The path to the created file. */ public static function createTemporary($inDirectoryPath = null, $namePrefix = self::DEFAULT_TEMPORARY_FILE_PREFIX) { assert('(!isset($inDirectoryPath) || is_cstring($inDirectoryPath)) && is_cstring($namePrefix)', vs(isset($this), get_defined_vars())); if (isset($inDirectoryPath)) { $inDirectoryPath = CFilePath::frameworkPath($inDirectoryPath); } else { $inDirectoryPath = CSystem::temporaryFilesDp(); } $filePath = tempnam($inDirectoryPath, $namePrefix); assert('is_cstring($filePath)', vs(isset($this), get_defined_vars())); self::setPermissions($filePath, self::DEFAULT_TEMPORARY_FILE_PERMISSIONS); return $filePath; }
public function testReFindDirectoriesOnNameRecursive() { $directoryPath = CFilePath::add(CSystem::temporaryFilesDp(), CFile::DEFAULT_TEMPORARY_FILE_PREFIX . self::$ms_tempDirName); if (CFile::exists($directoryPath)) { CFile::deleteDirectoryRecursive($directoryPath); } CFile::createDirectory($directoryPath); CFile::create(CFilePath::add($directoryPath, "file-a3")); CFile::create(CFilePath::add($directoryPath, "file-a20")); CFile::create(CFilePath::add($directoryPath, "file-a100")); CFile::create(CFilePath::add($directoryPath, "file-b3")); CFile::create(CFilePath::add($directoryPath, "file-b20")); CFile::create(CFilePath::add($directoryPath, "file-b100")); $directoryPathSub0 = CFilePath::add($directoryPath, "dir-a2"); $directoryPathSub1 = CFilePath::add($directoryPath, "dir-a10"); $directoryPathSub2 = CFilePath::add($directoryPath, "dir-b2"); $directoryPathSub3 = CFilePath::add($directoryPath, "dir-b10"); CFile::createDirectory($directoryPathSub0); CFile::createDirectory(CFilePath::add($directoryPathSub0, "dir-a2")); CFile::createDirectory(CFilePath::add($directoryPathSub0, "dir-a10")); CFile::createDirectory($directoryPathSub1); CFile::createDirectory(CFilePath::add($directoryPathSub1, "dir-a2")); CFile::createDirectory(CFilePath::add($directoryPathSub1, "dir-a10")); CFile::createDirectory($directoryPathSub2); CFile::createDirectory(CFilePath::add($directoryPathSub2, "dir-b2")); CFile::createDirectory(CFilePath::add($directoryPathSub2, "dir-b10")); CFile::createDirectory($directoryPathSub3); CFile::createDirectory(CFilePath::add($directoryPathSub3, "dir-b2")); CFile::createDirectory(CFilePath::add($directoryPathSub3, "dir-b10")); $paths = CFile::reFindDirectoriesOnNameRecursive($directoryPath, "/^.*-b/"); $comparator = function ($arrayString, $findString) { return $arrayString->endsWith($findString); }; $this->assertTrue($paths->length() == 6); $this->assertTrue($paths->find("/dir-b2", $comparator) && $paths->find("/dir-b10", $comparator) && $paths->find("/dir-b2/dir-b2", $comparator) && $paths->find("/dir-b2/dir-b10", $comparator) && $paths->find("/dir-b10/dir-b2", $comparator) && $paths->find("/dir-b10/dir-b10", $comparator)); $paths = CFile::reFindDirectoriesOnNameRecursive($directoryPath, "/^.*-b/", true); $this->assertTrue($paths->length() == 6); $this->assertTrue($paths[0]->endsWith("/dir-b2") && $paths[1]->endsWith("/dir-b10") && $paths[2]->endsWith("/dir-b2/dir-b2") && $paths[3]->endsWith("/dir-b2/dir-b10") && $paths[4]->endsWith("/dir-b10/dir-b2") && $paths[5]->endsWith("/dir-b10/dir-b10")); CFile::deleteDirectoryRecursive($directoryPath); }
public function testAbsolute() { $directoryPath = CFilePath::add(CSystem::temporaryFilesDp(), CFile::DEFAULT_TEMPORARY_FILE_PREFIX . self::$ms_tempDirName); if (CFile::exists($directoryPath)) { CFile::deleteDirectoryRecursive($directoryPath); } CFile::createDirectory($directoryPath); $filePathAbs = CFilePath::add($directoryPath, "file"); CFile::create($filePathAbs); CSystem::cd($directoryPath); $this->assertTrue(CFilePath::absolute("file")->equals(CFilePath::absolute($filePathAbs))); $this->assertTrue(CFilePath::absolute("./file")->equals(CFilePath::absolute($filePathAbs))); CFile::deleteDirectoryRecursive($directoryPath); }