本文整理汇总了PHP中SplFileInfo::openFile方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileInfo::openFile方法的具体用法?PHP SplFileInfo::openFile怎么用?PHP SplFileInfo::openFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileInfo
的用法示例。
在下文中一共展示了SplFileInfo::openFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: writeArray
/**
* @param string[] $items
* @return bool
*/
public function writeArray(array $items)
{
$content = $this->generateFileContentForArray($items);
$file = $this->fileInfo->openFile('w');
$file->fwrite($content);
return $file->fflush();
}
示例2: _createFeedLockFile
/**
* Create a file if is not already exists.
* @return self
*/
protected function _createFeedLockFile()
{
if (!$this->_isFeedLockFileExist()) {
$this->_stream->openFile(static::FILE_OPEN_MODE);
}
return $this;
}
示例3: getFile
/**
* @return \SplFileObject
*/
protected function getFile()
{
if (!$this->file instanceof \SplFileObject) {
$this->file = $this->fileInfo->openFile();
$this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE);
$this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
if ($this->firstLineIsHeader && !$this->header) {
$this->header = $this->file->fgetcsv();
}
}
return $this->file;
}
示例4: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$fileInfo = new \SplFileInfo($this->getContainer()->getParameter('kernel.root_dir') . '/../web/sitemap.xml');
if ($fileInfo->isFile() && $fileInfo->isReadable()) {
$output->write('Reading sitemap.xml...');
$file = $fileInfo->openFile();
$xml = '';
while (!$file->eof()) {
$xml .= $file->fgets();
}
$output->writeln(' done.');
$output->write('Updating sitemap.xml...');
$sitemap = new \SimpleXMLIterator($xml);
$sitemap->rewind();
$lastmodDate = new \DateTime();
$lastmodDate->sub(new \DateInterval('P1D'));
$lastmodDateFormatted = $lastmodDate->format('Y-m-d');
while ($sitemap->valid()) {
$sitemap->current()->lastmod = $lastmodDateFormatted;
$sitemap->next();
}
$file = $file->openFile('w');
$file->fwrite($sitemap->asXML());
$output->writeln(' done.');
} else {
$output->writeln('Error: Cannot open file web/sitemap.xml');
}
}
示例5: importSingleMovie
public function importSingleMovie(\SplFileInfo $source)
{
if (!$source->isReadable()) {
return $this->output->writelnError(sprintf("File %s not readable", $source->getFilename()));
}
$this->output->writelnInfo(sprintf("Importing %s ...", $source->getRealPath()));
$file = $source->openFile();
/** @var Movies $movie */
$movie = $this->getMovie($file->fread($file->getSize()), $file->getBasename('.html'));
if (!$movie) {
return $this->output->writelnError(sprintf("Not found dmm id", $source->getFilename()));
}
$this->currentDmmId = $movie->banngo;
if (Movies::findFirstById($movie->id)) {
return $this->output->writelnWarning(sprintf("Movie %s already exists, insert skipped", $movie->banngo));
}
/** @var Mysql $db */
$db = $this->getDI()->get('dbMaster');
$db->begin();
if (false === $movie->save()) {
$db->rollback();
return $this->output->writelnError(sprintf("Movie saving failed by %s", implode(',', $movie->getMessages())));
}
$db->commit();
$this->output->writelnSuccess(sprintf("Movie %s saving success as %s, detail: %s", $movie->banngo, $movie->id, ''));
}
示例6: getInput
protected function getInput()
{
$input = new \SplFileInfo(FILESYSTEM1 . self::KEY);
$fileObj = $input->openFile('a');
$fileObj->fwrite(self::CONTENT);
return $input;
}
示例7: validate
/**
* Main check method
*
* @param string $pathToFile
* @throws \RuntimeException if file not found
* @return bool
*/
public function validate($pathToFile)
{
$this->clearErrors();
$path = new \SplFileInfo($pathToFile);
if (!$path->isFile() || !$path->isReadable()) {
throw new \RuntimeException(sprintf('File "%s" not found', $pathToFile));
}
$file = $path->openFile('r');
$currentLineNumber = 1;
while (!$file->eof()) {
$line = $file->fgets();
if ($line == '' && $file->eof()) {
break;
}
// Validate line structure
$this->validateEOL($currentLineNumber, $line);
$nodes = array_filter(preg_split('/\\s+/', $line));
// Validate label
$label = array_shift($nodes);
$this->validateLabel($currentLineNumber, $label);
// Validate features format
$this->validateFeatures($currentLineNumber, $nodes);
// Increate the line number
$currentLineNumber++;
}
return $this->isValid();
}
示例8: it_throws_if_the_data_read_back_from_the_file_system_is_not_an_array
public function it_throws_if_the_data_read_back_from_the_file_system_is_not_an_array()
{
$fs = new FileSystem();
$fileInfo = new \SplFileInfo($fs->path('/r.php'));
$file = $fileInfo->openFile('w');
$file->fwrite("<?php\nreturn 'foo';");
$this->beConstructedWith($fileInfo);
$this->shouldThrow(UnexpectedValueException::class)->duringReadArray();
}
示例9: importXmlFile
public function importXmlFile(\SplFileInfo $source)
{
if (!$source->isReadable()) {
return $this->output->writelnError(sprintf("File %s not readable", $source->getFilename()));
}
$this->output->writelnInfo(sprintf("Importing %s ...", $source->getRealPath()));
$file = $source->openFile();
$xml = simplexml_load_string($file->fread($file->getSize()));
return $this->saveDmmList($xml);
}
示例10: append
/**
* @see EbayEnterprise_Catalog_Model_Error_IConfirmations::append()
*/
public function append($content = '')
{
if (is_null($this->fileStream)) {
throw new EbayEnterprise_Catalog_Model_Error_Exception(sprintf('[ %s ] Error Confirmations file stream is not instantiated', __CLASS__));
}
$oldMask = umask(self::ERROR_FILE_PERMISSIONS_MASK);
$this->fileStream->openFile('a')->fwrite("{$content}\n");
umask($oldMask);
return $this;
}
示例11: buildFromSplFileInfo
/**
* Builds a UnitOfWork instance from the given \SplFileInfo
*
* @param \SplFileInfo $fileInfo The fileInfo to build from
*
* @throws \InvalidArgumentException
*
* @return UnitOfWork The instantiated migration
*/
public static function buildFromSplFileInfo(\SplFileInfo $fileInfo)
{
$uniqueId = $fileInfo->getBasename();
$file = $fileInfo->openFile();
$query = '';
while (!$file->eof()) {
$query .= $file->fgets();
}
return new UnitOfWork(new Uid($uniqueId), new Workload($query), new \DateTime());
}
示例12: write_file
public static function write_file($path, $data, $mode = 'w')
{
try {
$file = new SplFileInfo($path);
$data_obj = $file->openFile($mode);
$data_obj->fwrite($data, strlen($data));
$data_obj->fflush();
} catch (Exception $e) {
throw new keke_exception($e->getMessage());
}
unset($file, $data_obj, $data);
return TRUE;
}
示例13: openFile
/**
* Abre um arquivo especificado pelo parâmetro $filePath para leitura.
* Retorna o objeto que representa o arquivo em caso de sucesso, ou o
* boolean false caso contrário.
*
* @param string $filePath caminho do arquivo
* @return mixed SplFileObject em caso de sucesso | bool false caso contrário
*/
static function openFile($filePath)
{
if (is_string($filePath)) {
try {
$fileInfo = new SplFileInfo($filePath);
if ($fileInfo->isFile()) {
$file = $fileInfo->openFile();
return $file;
} else {
return false;
}
} catch (Exception $e) {
trigger_error("Arquivo não pode ser importado\n" . $e->getMessage(), E_USER_WARNING);
return false;
}
}
}
示例14: fetchFile
/**
* Fetch a file from remote host and write to local filesystem.
*
* Set overwrite to true to force overwrite of existing files.
*
* @param string $remotePathName - e.g. replicant/files
* @param string $localPathName - e.g. assets/replicant/files
* @param bool $overwrite
* @param string $contentType
* @return int|bool number of bytes written or false if failed
* @throws Exception
*/
public function fetchFile($remotePathName, $localPathName, $overwrite = false, $contentType = '')
{
// most paths lead to null or error return
$written = null;
$fileInfo = new SplFileInfo($localPathName);
$exists = $fileInfo->isFile();
if ($exists && !$overwrite) {
return false;
}
$url = $this->buildURL($remotePathName);
$fileObject = $fileInfo->openFile("w");
if ($fileObject) {
// readFile will throw an exception on error so we'll not try and write a bad file
$written = $fileObject->fwrite($this->readFile($url, $contentType));
}
return $written > 0 ? $written : false;
}
示例15: get
/**
* Overwritten I do not want to throw an exception...just ignore it! return default and delete.
* Retrieve a cached value entry by id.
*
* // Retrieve cache entry from file group
* $data = Cache::instance('file')->get('foo');
*
* // Retrieve cache entry from file group and return 'bar' if miss
* $data = Cache::instance('file')->get('foo', 'bar');
*
* @param string $id id of cache to entry
* @param string $default default value to return if cache miss
* @return mixed
* @throws Cache_Exception
*/
public function get($id, $default = NULL)
{
$filename = Cache_File::filename($this->_sanitize_id($id));
$directory = $this->_resolve_directory($filename);
// Wrap operations in try/catch to return default
try {
// Open file
$file = new SplFileInfo($directory . $filename);
// If file does not exist
if (!$file->isFile()) {
// Return default value
return $default;
} else {
// Open the file and parse data
$created = $file->getMTime();
$data = $file->openFile();
$lifetime = $data->fgets();
// If we're at the EOF at this point, corrupted!
if ($data->eof()) {
$this->_delete_file($file, NULL, TRUE);
return $default;
}
$cache = '';
while ($data->eof() === FALSE) {
$cache .= $data->fgets();
}
// Test the expiry
if ($created + (int) $lifetime < time()) {
// Delete the file
$this->_delete_file($file, NULL, TRUE);
return $default;
} else {
return unserialize($cache);
}
}
} catch (ErrorException $e) {
$this->_delete_file($file, NULL, TRUE);
return $default;
}
}