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
17
votes
3 answers

SplFileObject vs fopen in PHP

What are the pros and cons of using fopen as opposed to SplFileObject in PHP? From what I see, SplFileObject throws exceptions where applicable which makes this convenient when using try...catch for error handling. Apart from this, are there any…
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
8
votes
0 answers

Get a stream resource from a SplFileInfo object

I'm calling a method that expects a stream resource for a file, like this: $obj->method(fopen($splFileInfo, 'r+')); The $splFileInfo object is an instance of SplFileInfo. Is there a way to get the stream resource of the $splFileInfo object without…
Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
7
votes
2 answers

Delete first X lines from a file PHP

I was wondering if anyone out there knew how this could be done in PHP. I am running a script that involves opening a file, taking the first 1000 lines, doing some stuff with those lines, then the php file opens another instance of itself to take…
Eric Strom
  • 715
  • 9
  • 20
7
votes
4 answers

RuntimeException SplFileInfo::getSize(): stat failed for... Laravel 4 upload image

I work with laravel 4 (last update), I create a form where we could upload an image (logo/avatar). I am on MAC OS, I use sublime Text 3, laravel and MAMP Applications. All my configuration is setting right, no running problems. My probleme is that I…
french_dev
  • 2,117
  • 10
  • 44
  • 85
6
votes
1 answer

Unlink and SplFileObject

Is it possible to unlink a file from an SplFileObject? I don't see a method to close the underlying resource, and the file handle is private so one can't extend SplFileObject with that goal in mind. Are there any workarounds?
JRL
  • 76,767
  • 18
  • 98
  • 146
5
votes
1 answer

php ; using fgetcsv with SplFileObject::fseek ; line read issue

When reading a specific line in a csv file, I tried to use SplFileObject::fseek with fgetcsv. To read line 2 (for example), I do a fseek(1) and read with fgetcsv, which gives line 2. When I do a fseek(0) and read with fgetcsv, I have line 0. So…
user2992220
  • 1,092
  • 1
  • 12
  • 20
5
votes
1 answer

SplFileObject::READ_CSV doesn't appear to be reading CSV file correctly - ignoring enclosure "

I have read documentation and searched this site extensively prior to asking this question. $file = new SplFileObject("/var/www/qacentre/compliance/compliance.csv"); $file->setFlags(SplFileObject::READ_CSV); foreach ($file as $row) { } Example CSV…
James Holz
  • 47
  • 1
  • 9
4
votes
2 answers

SplFileObject + LimitIterator + offset

I have data file with two lines (two lines just for my example, in real, that file can contain millions of lines) and I use SplFileObject and LimitIterator with offseting. But this combination have strange behaviour in some cases: $offset = 0; $file…
4
votes
1 answer

Write to file using SplFileObject()

I'm trying to edit a file by line using SplFileObject(). I can choose the line of the file I want to edit. But how do I then write a $string to that line? Here is my code to get the line:
user3143218
  • 1,738
  • 5
  • 32
  • 48
3
votes
1 answer

How to support non-standard characters in PHP SplFileObject reading a CSV

I have a short script that reads a CSV file which looks like the following: $csv = new SplFileObject($pathToFile, 'r'); while (!$csv->eof() && ($row = $csv->fgetcsv()) && $row[0] !== null) { var_dump($row); } This works ok, except it has a…
Jack
  • 33
  • 1
  • 3
2
votes
2 answers

Iterate through text file and check if file exist on server

I have a txt file with 40.000 file paths with filenames that I need to check if they exist. To check for a single file i use the following code: $filename='/home/httpd/html/domain.com/htdocs/car/002.jpg'; if (file_exists($filename)) { echo "The…
Philip
  • 23
  • 6
2
votes
0 answers

How to use SplFileObject() with FTP?

This code runs very well with fopen:
celsowm
  • 846
  • 9
  • 34
  • 59
1
vote
0 answers

First call to fgets in SplFileObject doesn't advance file key

Consider following text file, test.txt: 1 2 3 And following PHP code: key()); $line = $file->fgets(); var_dump($file->key()); $line = $file->fgets(); var_dump($file->key()); $line =…
Waiting for Dev...
  • 12,629
  • 5
  • 47
  • 57
1
vote
2 answers

How to assign SplFileObject::fpassthru output to variable

I'm currently writing some data to an SplFileObject like this: $fileObj = new SplFileObject('php://text/plain,', "w+"); foreach($data as $row) { $fileObj->fputcsv($row); } Now, I want to dump the whole output (string) to a variable. I know…
denormalizer
  • 2,186
  • 3
  • 24
  • 36
1
vote
2 answers

Search for text in a 5GB+ file then get whole line

I want to search for the text Hello (example) in a TXT file whose size is 5GB+ then return the whole line. I've tried using SplFileObject but what I know is that the line number is required to use SplFileObject, like that: $linenumber = 2094; $file…
Mario
  • 1,374
  • 6
  • 22
  • 48
1
2 3 4