Skip to content

Instantly share code, notes, and snippets.

@violetyk
Created October 1, 2012 05:33
Show Gist options
  • Save violetyk/3809625 to your computer and use it in GitHub Desktop.
Save violetyk/3809625 to your computer and use it in GitHub Desktop.
[cakephp]CSVアップロード
// ビュー
<?= $form->create(array('action' => 'upload', 'type' => 'file')) ?>
<?= $form->input('UI.file', array('type' => 'file')) ?>
<?= $form->submit('アップロード') ?>
<?= $form->end() ?>
<?php
// コントローラ
App::import('Vendor', 'Stream_Filter_Mbstring', array('file' => 'openpear/Stream/Filter/Mbstring.php'));
function upload() {
if (!empty($this->data)) {
if (is_uploaded_file($this->data['UI']['file']['tmp_name'])
&&
($fp = fopen($this->data['UI']['file']['tmp_name'],'r')) !== false
) {
$ret = stream_filter_register("convert.mbstring.*", "Stream_Filter_Mbstring");
$filter = stream_filter_append(
$fp,
'convert.mbstring.encoding.SJIS-win:UTF-8',
STREAM_FILTER_READ
);
$current_locale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'ja_JP.UTF-8');
$i = 0; while(($data = fgetcsv($fp)) !== false) {
print_r($data);
if (++$i > 5) {
break;
}
}
setlocale(LC_ALL, $current_locale);
fclose($fp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment