B
Bgag
Hallo zusammen!
Habe vor längerer Zeit mal eine Klasse zu diesem Thema geschrieben. Müsstet sie euch mal anschauen. Kann sie auch gerne nochmal die Tage überarbeiten.
MfG, Andy
Habe vor längerer Zeit mal eine Klasse zu diesem Thema geschrieben. Müsstet sie euch mal anschauen. Kann sie auch gerne nochmal die Tage überarbeiten.
MfG, Andy
PHP:
<?php
/**
* Class CsvHandler
*
* The CsvHandler class enables reading lines
* from csv-files. Furtehermore you are able to
* read the complete file into an array.
*
* @package CsvHandler
* @version 0.3
* @date 3/4/2008
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
* @see http://avedo.net
*/
class CsvHandler
{
/**
* @var String $file The filename of the forced file
* @access private
*/
private $file;
/**
* @var FilePointer $fp The file handle object
* @access private
*/
private $fp;
/**
* @var Array $data The data from the current file
* @access private
*/
private $data;
/**
* Creates a new csv-file
*
* @access public
* @param String $path The path to the csv file
* @return void
*/
public function create($path)
{
// create file
if( !file_exists($path) )
{
file_put_contents($path, '');
}
else
{
throw new Exception($path . ' already exists');
}
}
/**
* Opens a csv-file (default: reading)
*
* @access public
* @param String $file The path to the csv file
* @param Integer $cols The number of columns in the csv file
* @param String $mod The working modificator
* @return void
*/
public function open($file, $cols, $mod = 'r')
{
// set path to file
$this->file = $file;
// open file
$this->fp = fopen($this->file, $mod);
// check result
if( !$this->fp )
{
throw new Exception('Could not open ' . $file);
}
// read data from file into an array
$this->data = $this->read($cols);
}
/**
* Locks the csv-file (default: LOCK_SH)
*
* @access public
* @param String $mod The working modificator
* @return void
*/
public function lock($mod = LOCK_SH)
{
// check result
if( !flock($this->fp, $mod) )
{
throw new Exception('Could not lock file');
}
}
/**
* gReads a line of csv-file
*
* @access public
* @param Integer $cols The number of columns in the csv file
* @return Array
*/
public function getLine($cols)
{
// read line into an array
$line = fgetcsv($this->fp, 4096);
// check result
if( !$line )
{
throw new Exception('Could not read line');
}
// check num of fields
if( count($line) != $cols )
{
throw new Exception('Line has a invalid format');
}
//return line-array
return $line;
}
/**
* Writes a line into csv-file
*
* @access public
* @param Array $fields The names of the fields in the csv file
* @return void
*/
public function setLine($fields)
{
// write line into file
$line = fputcsv($this->fp, 4096);
// check result
if(!$line)
{
throw new Exception('Could not write line');
}
}
/**
* Adds a new line to csv-file
*
* @access public
* @param Array $fields The names of the fields in the csv file
* @return void
*/
public function add($fields)
{
// add new line to data-array
array_push($this->data, $fields);
}
/**
* Reads the whole csv-file
*
* @access public
* @param Integer $cols The number of columns in the csv file
* @return Array
*/
public function read($cols)
{
// get the whole data
while( !feof )
{
$data = $this->getLine($cols);
}
// return data-array
return $data;
}
/**
* Edits an entry in the csv-file
*
* @access public
* @param Integer $row The row in the csv file
* @param Integer $col The column in the data row
* @param Integer $value The value which should be assign
* @return Array
*/
public function edit($row, $col, $value)
{
//check number of rows
if( $row > count($this->data) )
{
throw new Exception("Line {$row} does not exist.");
}
// check num of fields
if( $col > count($this->data[$row-1]) )
{
throw new Exception("Column {$col} does not exist.");
}
// edit field
$this->data[$row-1][$col-1] = $value;
}
/**
* Saves whole data-array to file
*
* @access public
* @return void
*/
public function save()
{
// close csv-file
$this->close();
}
/**
* Closes csv-file
*
* @access public
* @return void
*/
public function close()
{
fclose($this->fp);
}
}
try
{
$fp = new CsvHandler('test.csv');
$fp->open();
$fp->lock();
$reply = $fp->getLine(4);
print_r($reply);
$fp->close();
}
catch(Exception $e)
{
echo "Error:<br />";
echo "<b>" . $e->getLine() . "</b>" . ": " . $e->getMessage() . "<br />";
}
?>