src/CsvLib.php
public static function removeRows($filepath, array $rowIndexes, $delimiter=",", $enclosure='"')
    {
        $tmpFile = tmpfile();
        $fileHandle = fopen($filepath, "r");
        $lineNumber = 1;
        
        if ($fileHandle)
        {
            while (!feof($fileHandle))
            {
                $lineArray = fgetcsv($fileHandle, 0, $delimiter, $enclosure);
                
                if ($lineArray)
                {
                    if (!in_array($lineNumber, $rowIndexes))
                    {
                        fputcsv($tmpFile, $lineArray, $delimiter, $enclosure);
                    }
                }
                
                $lineNumber++;
            }
            
            fclose($fileHandle);
            $meta_data = stream_get_meta_data($tmpFile);
            $tmpFileName = $meta_data["uri"];
            rename($tmpFileName, $filepath); # replace the old upload file with new.
        }
        else
        {
            throw new \Exception("removeRows: Failed to open CSV file for trimming.");
        }
    }
public static function removeRows($filepath, array $rowIndexes, $delimiter=",", $enclosure='"')
    {
        $tmpFile = tmpfile();
        $fileHandle = fopen($filepath, "r");
        $lineNumber = 1;
        
        if ($fileHandle)
        {
            while (!feof($fileHandle))
            {
                $lineArray = fgetcsv($fileHandle, 0, $delimiter, $enclosure);
                
                if ($lineArray)
                {
                    if (!in_array($lineNumber, $rowIndexes))
                    {
                        fputcsv($tmpFile, $lineArray, $delimiter, $enclosure);
                    }
                }
                
                $lineNumber++;
            }
            
            fclose($fileHandle);
            $meta_data = stream_get_meta_data($tmpFile);
            $tmpFileName = $meta_data["uri"];
            rename($tmpFileName, $filepath); # replace the old upload file with new.
        }
        else
        {
            throw new \Exception("removeRows: Failed to open CSV file for trimming.");
        }
    }
public static function removeRows ( $ filepath , array $ rowIndexes , $ delimiter = "," , $ enclosure = '"' ) { $ tmpFile = tmpfile ( ) ; $ fileHandle = fopen ( $ filepath , "r" ) ; $ lineNumber = 1 ; if ( $ fileHandle ) { while ( ! feof ( $ fileHandle ) ) { $ lineArray = fgetcsv ( $ fileHandle , 0 , $ delimiter , $ enclosure ) ; if ( $ lineArray ) { if ( ! in_array ( $ lineNumber , $ rowIndexes ) ) { fputcsv ( $ tmpFile , $ lineArray , $ delimiter , $ enclosure ) ; } } $ lineNumber ++ ; } fclose ( $ fileHandle ) ; $ meta_data = stream_get_meta_data ( $ tmpFile ) ; $ tmpFileName = $ meta_data [ "uri" ] ; rename ( $ tmpFileName , $ filepath ) ; # replace the old upload file with new. } else { throw new \ Exception ( "removeRows: Failed to open CSV file for trimming." ) ; } }
Remove rows from a CSV file.
@param string $filepath - the path of the CSV file we are modifying.
@param array $rowIndexes - array of integers specifying the row numbers to remove, starting at 1
@param string $delimiter - optionally specify the delimiter if it isn't a comma
@param string $enclosure - optionally specify the enclosure if it isn't double quote
Remove rows from a CSV file .