本文整理汇总了PHP中SplFileObject::ftruncate方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::ftruncate方法的具体用法?PHP SplFileObject::ftruncate怎么用?PHP SplFileObject::ftruncate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::ftruncate方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: write
/**
* Write data for key into cache
*
* @param string $key Identifier for the data
* @param mixed $data Data to be cached
* @return bool True if the data was successfully cached, false on failure
*/
public function write($key, $data)
{
if ($data === '' || !$this->_init) {
return false;
}
$key = $this->_key($key);
if ($this->_setKey($key, true) === false) {
return false;
}
$lineBreak = "\n";
if ($this->_config['isWindows']) {
$lineBreak = "\r\n";
}
if (!empty($this->_config['serialize'])) {
if ($this->_config['isWindows']) {
$data = str_replace('\\', '\\\\\\\\', serialize($data));
} else {
$data = serialize($data);
}
}
$duration = $this->_config['duration'];
$expires = time() + $duration;
$contents = $expires . $lineBreak . $data . $lineBreak;
if ($this->_config['lock']) {
$this->_File->flock(LOCK_EX);
}
$this->_File->rewind();
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
$this->_File = null;
return $success;
}
示例2: truncate
/**
* Truncates the collection, removing all the data from the file
*
* @return bool
*/
public function truncate()
{
$this->file->flock(LOCK_EX);
$result = $this->file->ftruncate(0);
$this->file->flock(LOCK_UN);
return $result;
}
示例3: write
public function write($key, $data, $cache_duration)
{
if ($data === '' || is_null($data) || !$this->is_init) {
return false;
}
if ($this->_openFile($key, true) === false) {
return false;
}
$line_break = "\n";
if ($this->settings['is_windows']) {
$lineBreak = "\r\n";
}
if (!empty($this->settings['serialize'])) {
if ($this->settings['is_windows']) {
$data = str_replace('\\', '\\\\\\\\', json_encode($data));
} else {
$data = json_encode($data);
}
}
$expires = time() + $cache_duration;
$contents = $expires . $line_break . $data . $line_break;
if ($this->settings['lock']) {
$this->file->flock(LOCK_EX);
}
$this->file->rewind();
$success = $this->file->ftruncate(0) && $this->file->fwrite($contents) && $this->file->fflush();
if ($this->settings['lock']) {
$this->file->flock(LOCK_UN);
}
return $success;
}
示例4: __destruct
/**
* Saves the current connection to `$this->file` if set.
*/
public function __destruct()
{
if (isset($this->file) && $this->file->isWritable()) {
$this->file->ftruncate(0);
$this->file->rewind();
$this->file->fwrite($this->toJson());
}
}
示例5: _write
private function _write()
{
$this->_file->ftruncate(0);
foreach ($this->_data as $domain => $stats) {
foreach ($stats as $date => $row) {
$this->_file->fputcsv([$domain, $date] + $row);
}
}
}
示例6: freeUsedRequestCount
/**
* Free the used slots
*
* @param $sliced_report_definitions_count
*/
private function freeUsedRequestCount($sliced_report_definitions_count)
{
$this->file_counter->flock(LOCK_EX);
$this->file_counter->rewind();
$used_request_count = (int) $this->file_counter->fgets();
$new_used_request_count = $used_request_count - $sliced_report_definitions_count;
$this->file_counter->ftruncate(0);
$this->file_counter->fwrite($new_used_request_count);
$this->file_counter->flock(LOCK_UN);
}
示例7: 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);
}
示例8: save
/**
* Writes the record data from memory into the file store.
*
* @return void
*/
public function save()
{
$tmpHandle = new \SplFileObject('php://temp', 'w+');
$tmpData = '';
$tmpHandle->fputcsv($this->model->columns());
foreach ($this->all() as $record) {
$tmpHandle->fputcsv($record->toArray());
$record->exists = true;
}
$tmpHandle->rewind();
while (!$tmpHandle->eof()) {
$tmpData .= $tmpHandle->fgets();
}
$this->file->flock(\LOCK_EX | \LOCK_NB);
$this->file->ftruncate(0);
$this->file->rewind();
$this->file->fwrite($tmpData);
$this->file->flock(\LOCK_UN);
}
示例9: dirname
<?php
set_include_path(dirname(dirname(__FILE__)));
$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fileobject_005.txt';
touch($path);
$fo = new SplFileObject('tests' . DIRECTORY_SEPARATOR . 'fileobject_005.txt', 'w+', true);
$fo->fwrite("blahlubba");
var_dump($fo->ftruncate(4));
$fo->rewind();
var_dump($fo->fgets(8));
$fo->rewind();
$fo->fwrite("blahlubba");
// This should throw a warning and return NULL since an argument is missing
var_dump($fo->ftruncate());
?>
==DONE==
示例10: SplFileObject
<?php
//create a basic stream class
class VariableStream
{
var $position;
var $varname;
function stream_open($path, $mode, $options, &$opened_path)
{
return true;
}
function url_stat()
{
}
}
stream_wrapper_register("SPLtest", "VariableStream");
$ftruncate_test = "";
//end creating stream
//open an SplFileObject using the above test stream
$obj = new SplFileObject("SPLtest://ftruncate_test");
try {
$obj->ftruncate(1);
} catch (LogicException $e) {
echo $e->getMessage();
}
示例11: updateOrDelete
private function updateOrDelete($id, $data = null)
{
/* Thanks to https://www.daniweb.com/web-development/php/threads/102279/deleting-a-line-from-a-file#post1353582 */
/*
* Create a new SplFileObject representation of the file
* Open it for reading and writing and place the pointer at the beginning of the file
* @see fopen for additional modes
*/
$file = new \SplFileObject($this->filename, 'a+');
/*
* Set a bitmask of the flags to apply - Only relates to reading from file
* In this case SplFileObject::DROP_NEW_LINE to remove new line charachters
* and SplFileObject::SKIP_EMPTY to remove empty lines
*/
$file->setFlags(7);
/*
* Lock the file so no other user can interfere with reading and writing while we work with it
*/
$file->flock(LOCK_EX);
/*
* Create a SplTempFileObject
* 0 indicates not to use memory to store the temp file.
* This is probably slower than using memory, but for a large file it will be much more effective
* than loading the entire file into memory
* @see http://www.php.net/manual/en/spltempfileobject.construct.php for more details
*/
$temp = new \SplTempFileObject(0);
/*
* Lock the temp file just in case
*/
$temp->flock(LOCK_EX);
/*
* Iterate over each line of the file only loading one line into memory at any point in time
*/
foreach ($file as $key => $line) {
if ($key != $id) {
/*
* If this line does NOT match out delete write it to the temp file
* Append a line ending to it
*/
$temp->fwrite($line . PHP_EOL);
} else {
if ($data !== null) {
$temp->fwrite($data . PHP_EOL);
}
}
}
/*
* Truncate the existing file to 0
*/
$file->ftruncate(0);
/*
* Write the temp file back to the existing file
*/
foreach ($temp as $line) {
/*
* Iterate over temp file and put each line back into original file
*/
$file->fwrite($line);
}
/*
* Release the file locks
*/
$temp->flock(LOCK_UN);
$file->flock(LOCK_UN);
return true;
}
示例12: testClear
/**
* @return void
*/
public function testClear()
{
$filePath = __DIR__ . '/example_write1.txt';
$fileObject = new FileWriter($filePath, 'a+');
$fileObject->clear();
$filePath = __DIR__ . '/example_write2.txt';
$fileObject = new \SplFileObject($filePath, 'a+');
$fileObject->ftruncate(0);
}
示例13: bufferCallback
private function bufferCallback($chunk, $flags)
{
$chunkSize = strlen($chunk);
if ($this->verbose) {
$level = ob_get_level();
$constants = array('PHP_OUTPUT_HANDLER_START', 'PHP_OUTPUT_HANDLER_WRITE', 'PHP_OUTPUT_HANDLER_FLUSH', 'PHP_OUTPUT_HANDLER_CLEAN', 'PHP_OUTPUT_HANDLER_FINAL');
$flagsText = '';
foreach ($constants as $i => $constant) {
if ($flags & ($value = constant($constant)) || $value == $flags) {
$flagsText .= (strlen($flagsText) ? ' | ' : '') . $constant . "[{$value}]";
}
}
$total = $this->bufferedCounter;
file_put_contents('php://stderr', sprintf("Buffer Callback: Chunk Size %s; Total %s; Flags %s (%s); Level %d\n", number_format($chunkSize), number_format($total + $chunkSize), $flags, $flagsText, $level));
}
if ($flags & PHP_OUTPUT_HANDLER_FINAL) {
return TRUE;
}
if ($flags & PHP_OUTPUT_HANDLER_START) {
$this->store->fseek(0, SEEK_END);
}
$chunkSize && $this->store->fwrite($chunk);
$this->bufferedCounter += $chunkSize;
if ($flags & PHP_OUTPUT_HANDLER_FLUSH) {
// there is nothing to d
}
if ($flags & PHP_OUTPUT_HANDLER_CLEAN) {
$this->store->ftruncate(0);
}
return "";
}
示例14: compileJavascript
/**
* Compiles the javascript files for the passed shop template.
*
* @param $timestamp
* @param Shop\Template $template
* @param Shop\Shop $shop
*/
public function compileJavascript($timestamp, Shop\Template $template, Shop\Shop $shop)
{
if ($shop->getMain()) {
$shop = $shop->getMain();
}
$file = $this->pathResolver->getJsFilePath($shop, $timestamp);
$file = new \SplFileObject($file, "a");
if (!$file->flock(LOCK_EX)) {
return;
}
$file->ftruncate(0);
$settings = $this->service->getSystemConfiguration(AbstractQuery::HYDRATE_OBJECT);
$javascriptFiles = $this->collectJavascriptFiles($template, $shop);
$content = '';
foreach ($javascriptFiles as $jsFile) {
$content .= file_get_contents($jsFile) . "\n";
}
if ($settings->getCompressJs()) {
$content = $this->jsCompressor->compress($content);
}
$file->fwrite($content);
$file->flock(LOCK_UN);
// release the lock
}