Questions tagged [splfileobject]

SplFileObject is a PHP class which offers an object oriented interface for a file.

SplFileObject is part of the Standard PHP Library (SPL). It offers an object oriented interface for a file. Documentation can be found here. It is available by default from PHP 5.1.0 onwards.

48 questions
1
vote
1 answer

1000 writes (appendings) to single file with SplFileObject

"receiver.php" file receives ±1000 Ajax post requests per second with $array data which is written with following code to a file.csv: $file = new SplFileObject( __DIR__ . 'file.csv', 'a' ); $file->fputcsv( $array, "|", "'" ); $file =…
Sid
  • 4,302
  • 3
  • 26
  • 27
1
vote
0 answers

SplFileObject iteration duplicates key 0

All, I cannot for the life of me, figure out why \SplFileObject is repeating the key in the following code. This sample uses Nowdoc as a file stream but this effect can be noted using any file. PHP Code: $str = <<<'EOD' Line 1 Line 2 Line 3 Line…
Curtis
  • 43
  • 1
  • 7
1
vote
0 answers

setting Timeout for SplFileObject reading from remote aws s3 file

I am reading a file line by line directly from aws s3 server using SplFileObject. $url = "s3://{$bucketName}/{$fileName}"; $fileHandle = new \SplFileObject($url, 'r'); $fileHandle->setFlags(SplFileObject::READ_AHEAD); while(!$fileHandle->eof()){ …
Waku-2
  • 1,136
  • 2
  • 13
  • 26
1
vote
1 answer

splfileobject not working on ubuntu 16.04 apache webserver with php7.0

I have been making a website that has a user-account-loggin system that is based on accessing .txt files in php. I originally tested much of the code on a free web-hosting service and got the user-account-loggin system functional, but when I tried…
hamman247
  • 11
  • 1
1
vote
1 answer

Regex to delete faulty characters within a csv file to make SplFileObject work correctly in PHP

I try to parse a csv file in PHP via SplFileObject. Sadly SplFileObject stucks sometimes if there are erroneous invisible characters in the text. The function detects a quote instead of skipping or read it as normal character while iterating over…
koseduhemak
  • 523
  • 2
  • 4
  • 19
1
vote
1 answer

Is there a faster way to import data from a file to MySQL using php?

Okay, so I get around 100k-1M lines of text that I always import to a database. The code that i use is as follows: $lines = new SplFileObject('/home/file.txt'); while(!$lines->eof()) { $lines->next(); //Skipping first line $row =…
1
vote
0 answers

Read log file with SplFileObject Class and flag SKIP_EMPTY not work

I need to read some rsync log files, and these have some empty lines I try to use: $f = new \SplFileObject($file); $f->setFlags(7); // Test 6 value and same problem while (!$f->eof()) { // Do something // But $f->fgets() show any…
abkrim
  • 3,512
  • 7
  • 43
  • 69
1
vote
0 answers

maximum execution time exceeded by fwrite method

I am trying to write to some txt files using SplFileObject's fwrite method and getting below error message. Fatal error: Maximum execution time of 30 seconds exceeded in /Users/oliverwilliams/Desktop/olliephp/directory.php on line 9. Line 9 is…
Ollie_W
  • 337
  • 1
  • 5
  • 13
1
vote
1 answer

PHP - SplFileObject - Wrong output for second line when using seek() method

Go to UPDATE to read what's the actual problem now. Old question was already resolved with the first answer submitted by Bert Peters. OLD QUESTION: I have few files named as file.1.txt, file.2.txt, file.3.txt, ... I'm reading first file with…
user1257255
  • 1,161
  • 8
  • 26
  • 55
1
vote
3 answers

Append string to a file at certain position

say we have this file test.txt: {} And we want to add text between { and }. I tried this: $f = new SplFileObject($filepath, 'r+'); $f->fseek(1); $f->fwrite('test'); but the output is {test, I tried also other modes like w+ (result-> test), a+…
Almis
  • 3,684
  • 2
  • 28
  • 58
1
vote
1 answer

creating multiple instances of splFileObject

I have a class similar to this class x { function __construct($file){ $this->readData = new splFileObject($file); } function a (){ //do something with $this->readData; } function b(){ //do something with $this->readData; } } $o…
user2679413
  • 49
  • 1
  • 7
1
vote
1 answer

File Overwrite issue when trying to transfer file using FTP

I have a FTP server with only List and Put permissions. But not having delete, overwrite and Rename permissions. Now when I try to transfer a file using simple FTP using the following implementation private boolean…
Chinni
  • 137
  • 3
  • 8
1
vote
2 answers

PHP LimitIterator fails ("Does not support seeking" + "Cannot rewind file")

I use SplFileObject and LimitIterator to read content from position x till y of a big file. This works perfectly when using a file path like /home/devel/stuff/myfile.log. When using a path like http://mydomain.com:8090/devel/stuff/myfile.log it does…
Shlomo
  • 3,880
  • 8
  • 50
  • 82
0
votes
0 answers

Permission denied for SplFileObject

I'm running php 5.3.8, on xampp (the newest version, 1.7.7 I believe), on Windows7 64bit. When I try to construct a SplFileObject giving it a directory path I get following message (both in read, or write mode): failed to open stream: Permission…
marek
  • 259
  • 8
  • 19
0
votes
1 answer

tab-delimited string to XML with PHP

I'm trying to write the tab-delimited content of a variable to XML like this: $tsvData = str_getcsv($input, "\t"); foreach($tsvData as $line => $row) { if($line > 0) { $xmlWriter->writeElement('NAME', $row[0]); …
Matt
  • 23
  • 1
  • 4