Skip to content

Instantly share code, notes, and snippets.

@bz0
Last active September 30, 2017 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/832cce91b5d6fe2b69ed0651fc9bb075 to your computer and use it in GitHub Desktop.
Save bz0/832cce91b5d6fe2b69ed0651fc9bb075 to your computer and use it in GitHub Desktop.
CSV(SJIS)読込クラスです。
<?php
require_once(dirname(__FILE__) . "/FailedOpenFileException.php");
require_once(dirname(__FILE__) . "/PermissionDeniedException.php");
require_once(dirname(__FILE__) . "/TempFileException.php");
class csv{
private $csvPath;
private $tempFilePath;
private $file;
private $temp;
public function __construct($csvPath){
$this->csvPath = $csvPath;
}
/*
* CSVファイル読み込み
* ファイルの文字コードは「SJIS」前提
*/
public function read(){
ob_start();
$csv = file_get_contents($this->csvPath);
$warning = ob_get_contents();
ob_end_clean();
if ($warning) {
$this->exceptionHandler($warning);
}
return $csv;
}
public function exceptionHandler($warning){
if(strpos($warning, '許可がありません') !== false){
$message = "ファイルにアクセスできませんでした。パーミッションを確認して下さい<br>filePath:" . $this->csvPath;
throw new FailedOpenFileException($message);
}else if (strpos($warning, 'そのようなファイルやディレクトリはありません') !== false){
$message = "ファイルが存在しません。パスを確認して下さい<br>filePath:" . $this->csvPath;
throw new PermissionDeniedException($message);
}
}
/*
* 文字コード変換(SJIS-win→UTF-8)
* @param string $csv CSVテキスト(SJIS)
*/
public function convert($csv){
$csv = mb_convert_encoding($csv, 'UTF-8', 'SJIS-win');
return $csv;
}
/*
* 一時ファイルに出力
* @param string $csv CSVテキスト(UTF-8)
*/
public function tempFileCreate($csv){
$this->temp = tmpfile();
if ($this->temp !== FALSE){
$tempInfo = stream_get_meta_data($this->temp);
$tempFilePath = $tempInfo['uri'];
fwrite($this->temp, $csv);
rewind($this->temp);
}else{
$message = "テンポラリファイルの生成に失敗しました";
throw new TempFileException($message);
}
return $tempFilePath;
}
/*
* 一時ファイルを閉じる
*/
public function tempFileClose(){
fclose($this->temp);
}
/*
* splFileObject設定
* @param string $tempFilePath
*/
public function config($tempFilePath){
$this->file = new SplFileObject($tempFilePath);
$this->file->setFlags(SplFileObject::READ_CSV);
$this->file->setCsvControl(',', '"', '"');
}
/*
* splFileObject PHP配列化
*/
public function convertArray(){
$csvArray = array();
foreach($this->file as $row) {
$csvArray[] = $row;
}
$this->file = null;
return $csvArray;
}
/*
* CSV→PHP配列に変換
*/
public function csvArrayConvert(){
$csv = $this->read();
$csv = $this->convert($csv);
$tempFilePath = $this->tempFileCreate($csv);
$this->config($tempFilePath);
$this->tempFileClose();
$csvArray = $this->convertArray();
return $csvArray;
}
}
<?php
ini_set("display_errors", 1);
error_reporting(-1);
header('Content-Type: text/html; charset=UTF-8');
mb_language('Japanese');
setlocale( LC_ALL, 'ja_JP.UTF-8' );
require_once("./csv.php");
$csvPath = "example.csv";
try{
$csv = new csv($csvPath);
$data = $csv->csvArrayConvert();
}catch(FailedOpenFileException $e){
//ファイルが存在しません
echo $e->getMessage();
exit;
}catch(PermissionDeniedException $e){
//パーミッションエラー
echo $e->getMessage();
exit;
}catch(TempFileException $e){
//テンポラリ(一時ファイル)ファイル生成失敗
echo $e->getMessage();
exit;
}catch(Exception $e){
//その他エラー
echo $e->getMessage();
}
echo "<pre>";
print_r($data);
echo "</pre>";
<?php
class FailedOpenFileException extends Exception
{
public function __construct($message) {
parent::__construct($message);
}
}
<?php
class PermissionDeniedException extends Exception
{
public function __construct($message) {
parent::__construct($message);
}
}
<?php
class TempFileException extends Exception
{
public function __construct($message) {
parent::__construct($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment