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
0
votes
0 answers

Behavior of fgetcsv through SplFileObject is not the same from PHP 8.0 to 8.1 - how to stay backwards compatible?

I have something like that in my code, which is working well since a few years : $spl_file = new SplFileObject($file_path, 'r', false, null); $spl_file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD); $result =…
0
votes
0 answers

PHP flock not having any effect in ubuntu

I have one php process locking a file with flock. I then start another process that deletes the file. On windows, I get the correct behaviour where the locked file is not able to be deleted, however on ubuntu (both wsl and a full Ubuntu…
Brad
  • 741
  • 7
  • 17
0
votes
1 answer

PHP reading next line in text file while using lazy loading?

I have a text file that I want to read line by line. For each line I also need the value from the next line, so I need something to read the next line while in the current line. I have read through many answers here that I can do this with a foreach…
reddy
  • 1,721
  • 3
  • 16
  • 26
0
votes
0 answers

PHP - SplFileObject write CSV in the first line

How can I write the following array in CSV format into the specific line of my file. I'm using the PHP LimitIterator to retrieve a part of my file. $fields = ['value_1', 'value_2', 'value_3']; $fileObject = new SplFileObject('my/file/path',…
Brah0um
  • 315
  • 2
  • 15
0
votes
1 answer

RuntimeException SplFileInfo::getSize(): stat failed for… Laravel 6 upload image

I work with laravel 6 (last update), I create a form where we could upload an image. I am on Windows OS, I use PHPStorm, laravel and xampp Applications. All my configuration is setting right, no running problems. My probleme is that I have this…
Ahmed Malek
  • 186
  • 1
  • 1
  • 8
0
votes
1 answer

SplFileObject not reading CSV properly

So I have to read multiple CSV files, merge their headers(by removing duplicates) and put values in a final(merged) CSV file. I am trying to use SplFileObject for the first time. I started by reading a file. Below are…
Volatil3
  • 14,253
  • 38
  • 134
  • 263
0
votes
2 answers

Why does this print the last line? (SplFileObject)

$file1 = new \SplFileObject('some file'); while( !$file1->eof() ){ $data = $file1->fgets(); if( !$file1->eof() ){ echo $data; } } Doesn't fgets() step to the next line in the file? If so why does the file still pass the second…
jrgilman
  • 460
  • 1
  • 4
  • 15
0
votes
0 answers

huge file to load "file vs SplFileObject" in php

I am trying to read a text file with 7 million lines hereby named HUGE. I am trying to read the first 10 thousand lines and then delete them. I am presently using SplFileObject like this... $file = new SplFileObject(FILENAME); for ($i =…
Mallik Kumar
  • 540
  • 1
  • 5
  • 28
0
votes
2 answers

PHP mkdir of a SPLFileObject

I'm trying to create n directories based on a TXT file using this code: What I expect is mkdir…
Meeku
  • 53
  • 2
  • 7
0
votes
0 answers

Parse tabulation-based data file php

I have several files to parse (with PHP) in order to insert their respective content in different database tables. First point : the client gave me 6 files, 5 are CSV with values separated by coma ; The last one do not come from the same database…
Flo Schild
  • 5,104
  • 4
  • 40
  • 55
0
votes
1 answer

Undefined offset using list()

I'm parsing a large CSV file using SplFileObject. This CSV has almost 100,000 records and mutliple columns. Some of these columns are empty. I have the following code: $file = new…
sipher_z
  • 1,221
  • 7
  • 25
  • 48
0
votes
0 answers

php not opening file on mounted filesystem

I have the following bit of code that is giving me problems. #$fullfilename="/data/extract/".$curpkg."/".$curfilename; $fullfilename = "/tmp/test.txt"; $readline = 0; $lictext=""; try { $file = new SplFileObject($fullfilename); …
0
votes
1 answer

SplFileObject __toString inheritance

I've got a class that extends SplFileObject and implements a __toString method, but I seem to get inconsistent behaviour, depending on how the method is called. Sometimes the parent method is called, and sometimes the child method. Can anyone…
jjok
  • 601
  • 5
  • 12
0
votes
1 answer

Why is my php flock not detect an existing lock when running command twice?

A little explanation, I have a Symfony2 setup. I'm using an abstract command class that I extend. I want any of those batches to be able to run only once. My goal is to make a lock file which I open and flock so that the lock is automatically…
Anyone
  • 2,814
  • 1
  • 22
  • 27
0
votes
3 answers

$file->eof() always returning false when using PHP's SplFileObject in 'r' mode

Why is my PHP script hanging? $path = tempnam(sys_get_temp_dir(), '').'.txt'; $fileInfo = new \SplFileInfo($path); $fileObject = $fileInfo->openFile('a'); $fileObject->fwrite("test line\n"); $fileObject2 =…
mattalxndr
  • 9,143
  • 8
  • 56
  • 87