Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace type;
- use Exception;
- class Csv{
- private $hnd;
- public $error=true;
- function __construct($filePath){
- $this->hnd = fopen($filePath, "r");
- if($this->hnd===false){
- $error=true;
- throw new Exception('fopen');
- return;
- }
- if(!flock($this->hnd,LOCK_SH)){
- throw new Exception('flock');
- }
- }
- function read(){
- $ln=fgets($this->hnd,1024);
- if(($ln===false)or(($ln===null)))return false;
- $row=str_getcsv($ln,';');
- if(sizeof($row)===1)$row=str_getcsv($ln,',');
- array_walk_recursive($row,
- function (&$item, $key) {
- $c=mb_detect_encoding($item,array('UTF-8','Windows-1251'));
- if($c<>'UTF-8')$item = iconv(mb_detect_encoding($item,array('Windows-1251','UTF-8')),"UTF-8",$item);
- }
- );
- return $row;
- }
- function __destruct(){
- flock($this->hnd,LOCK_UN);
- fclose($this->hnd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement