本文整理汇总了PHP中SplFileObject::fread方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::fread方法的具体用法?PHP SplFileObject::fread怎么用?PHP SplFileObject::fread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::fread方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* File constructor.
* @param $uri String File path or URL
* @throws \ErrorException
* @throws \Exception
*/
public function __construct($uri)
{
try {
$this->messageFactory = new MessageFactory();
$file = new \SplFileObject($uri, 'rb');
while ($file->valid()) {
$raw_time = $file->fread(8);
if (!$raw_time) {
break;
}
$header = new Header($file->fread(6));
if ($header->magic !== 0xfe) {
throw new \ErrorException("Unexpected magic number ({$header->magic})");
}
$payload = $file->fread($header->length);
$raw_crc = $file->fread(2);
$entry = new Entry($raw_time, $header, $payload, $raw_crc);
$this->entries[] = $entry;
$this->messages[] = $this->messageFactory->build($entry);
}
if (!$this->hasEntries()) {
throw new \ErrorException('No entries found in file.');
}
$this->startTime = $this->entries[0]->time;
$this->endTime = end($this->entries)->time;
} catch (\Exception $ex) {
// echo $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine();
die;
}
}
示例2: getCoverageObject
/**
* Returns coverage object from file.
*
* @param \SplFileObject $coverageFile Coverage file.
*
* @return \PHP_CodeCoverage|CodeCoverage
*/
private function getCoverageObject(\SplFileObject $coverageFile)
{
if ('<?php' === $coverageFile->fread(5)) {
return include $coverageFile->getRealPath();
}
$coverageFile->fseek(0);
// the PHPUnit 3.x and below
return unserialize($coverageFile->fread($coverageFile->getSize()));
}
示例3: __construct
/**
* コンストラクタ
* 副作用が大量にあるので注意
*/
public function __construct(ClientInterface $client, $filename = 'stamp.json', $span = 0, $mark_limit = 10000, $back_limit = 3600)
{
// ヘッダの送出
if (!headers_sent()) {
header('Content-Type: text/html; charset=UTF-8');
}
// エラー表示の設定
error_reporting(-1);
ini_set('log_errors', PHP_SAPI === 'cli');
ini_set('display_errors', PHP_SAPI !== 'cli');
// 重複起動防止
$file = new \SplFileObject($filename, 'a+b');
if (!$file->flock(LOCK_EX | LOCK_NB)) {
throw new \RuntimeException('Failed to lock file.');
}
// JSONとして保存してあるデータを取得
clearstatcache();
$json = $file->getSize() > 0 ? json_decode($file->fread($file->getSize()), true) : [];
// JSONに前回実行時刻が保存されていた時
if (isset($json['prev'])) {
// 十分に時間が空いたかどうかをチェック
if (!static::expired($json['prev'], $span)) {
throw new \RuntimeException('Execution span is not enough.');
}
}
// JSONにマーク済みステータス一覧が記録されていたとき復元する
if (isset($json['marked'])) {
$this->marked = array_map('filter_var', (array) $json['marked']);
}
$this->client = $client;
$this->file = $file;
$this->prev = (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('r');
$this->mark_limit = $mark_limit;
$this->back_limit = $back_limit;
}
示例4: content
private function content(SplFileObject $file)
{
$result = "";
$file->fseek(0);
while (!$file->eof()) {
$result .= $file->fread(1024);
}
return $result;
}
示例5: readComplexRSS
private function readComplexRSS()
{
$filename = $this->createComplexRSS();
$file = new \SplFileObject($filename, "r");
$contentToTest = $file->fread($file->getSize());
$filename = 'tests/RSS/ComplexRSS.rss';
$file = new \SplFileObject($filename, "r");
$content = $file->fread($file->getSize());
return array($contentToTest, $content);
}
示例6: read
/**
* @param integer $length
* @return mixed
*/
public function read($length)
{
if (version_compare(phpversion(), '5.5.11', '<')) {
trigger_error(sprintf('%s is not supported on PHP %s < 5.5.11.', __METHOD__, phpversion()), E_USER_WARNING);
$buffer = '';
while (strlen($buffer) < $length && !$this->eof()) {
$buffer .= $this->fgets();
}
return $buffer;
}
return parent::fread($length);
}
示例7: getMetadata
/**
* Retrieves the Metadata for a Key/Value pair
*
* Optionally allows a specific position offset in the file
* Return Array
* - klen: The length of the Key
* - vlen: The length of the Value
* - length: The total combined length of the Key and the Value
*
* @param integer $position An offset to seek to in a file
*
* @return array|bool
*/
protected function getMetadata($position = null)
{
if (!is_null($position)) {
$this->file->fseek($position);
}
$metadata = $this->file->fread(8);
if ($metadata) {
list(, $klen, $vlen) = unpack('N*', $metadata);
return (object) ['klen' => $klen, 'vlen' => $vlen, 'length' => $klen + $vlen];
}
return false;
}
示例8: getTotal
/**
* @return int
*/
public function getTotal()
{
if ($this->total === null) {
$this->csvFile->fseek(0);
$lines = 0;
while (!$this->csvFile->eof()) {
$lines += substr_count($this->csvFile->fread(8192), $this->lineSeparator);
}
$this->total = (int) $lines;
}
return $this->total;
}
示例9: readClasses
/**
* Read all classes from path
*
* @return array $classes
*/
protected function readClasses()
{
$classes = array();
$directory = new RecursiveDirectoryIterator($this->pathTop, RecursiveDirectoryIterator::SKIP_DOTS);
$fileIterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($fileIterator as $file) {
$fileClass = new SplFileObject($file->getPathname());
$fileExtension = new SplFileInfo($file->getPathname());
if ($fileExtension->getExtension() == $this->fileExt) {
$classes[] = $fileClass->fread($fileClass->getSize());
}
}
return $classes;
}
示例10: write
/**
* {@inheritdoc}
*/
public function write(\SplFileObject $file, $text, $newLineAtEnd = false)
{
$originalSeek = $file->ftell();
// Refresh file size
clearstatcache($file->getFilename());
if ($file->getSize() - $file->ftell() > 0) {
$contentAfter = $file->fread($file->getSize() - $file->ftell());
} else {
$contentAfter = '';
}
$file->fseek($originalSeek);
$file->fwrite($text . ($newLineAtEnd ? PHP_EOL : ''));
$file->fwrite($contentAfter);
return $file;
}
示例11: skipHead
/**
* It skips the file headers
* @return boolean
* @access private
* @final
*/
private final function skipHead()
{
if ($this->fileObject->getSize() < 0) {
return false;
}
$this->fileObject->rewind();
$this->fileObject->fseek(7, SEEK_CUR);
$mainHead = $this->fileObject->fread(7);
if (ord($mainHead[2]) != 0x73) {
return false;
}
$headSize = $this->getBytes($mainHead, 5, 2);
$this->fileObject->fseek($headSize - 7, SEEK_CUR);
unset($mainHead, $headSize);
return true;
}
示例12: finalizeFile
public function finalizeFile(\SplFileObject $fileObject)
{
if ($this->headers !== null) {
// Create tmp file with header
$fd = fopen('php://temp', 'w+b');
fputcsv($fd, $this->headers);
// Copy file content into tmp file
$fileObject->rewind();
fwrite($fd, $fileObject->fread($fileObject->getSize()));
// Overwrite file content with tmp content
rewind($fd);
$fileObject->rewind();
$fileObject->fwrite(stream_get_contents($fd));
clearstatcache(true, $fileObject->getPathname());
fclose($fd);
}
// Remove last line feed
$fileObject->ftruncate($fileObject->getSize() - 1);
}
示例13: fread
/**
* @inheritDoc
*/
public function fread($length)
{
$buffer = parent::fread($length);
if ($this->eof()) {
return $buffer;
}
$byte = ord(substr($buffer, -1, 1));
if ($byte <= 0x7f) {
return $this->fixLength($buffer, $length);
}
$count = 1;
while ($byte < 0xc0 && $count < $length) {
$count++;
$byte = ord(substr($buffer, -$count, 1));
}
$mask = 0x40;
$bitsCount = 1;
while ($byte & $mask) {
$mask = $mask >> 1;
$bitsCount++;
}
$bytesToEnd = $bitsCount - $count;
return $this->fixLength($buffer . ($bytesToEnd ? parent::fread($bytesToEnd) : ''), $length);
}
示例14: SplFileObject
<?php
$obj = new SplFileObject(__FILE__, 'r');
$data = $obj->fread(5);
var_dump($data);
$data = $obj->fread();
var_dump($data);
$data = $obj->fread(0);
var_dump($data);
// read more data than is available
$data = $obj->fread(filesize(__FILE__) + 32);
var_dump(strlen($data) === filesize(__FILE__) - 5);
示例15: fromFile
/**
* Unserializes the object from JSON contained in the given file.
*
* @param \SplFileObject $file
* The file to be read from. Since it may also be written to, it should
* be opened with mode `c+`.
*
* @return DivideIQ
* The unserialized object.
*/
public static function fromFile(\SplFileObject $file)
{
$file->rewind();
$object = static::fromJson($file->fread($file->getSize()));
$object->file = $file;
return $object;
}