1
A
回答
2
您可能想看看流式傳輸csv文件。發送啓動文件位置,起始位置和字節數改爲得到paramters到ProgressiveReader.php
class NoFileFoundException extends Exception {
function __toString() {
return '<h1><b>ERROR:</b> could not find ('
.$this->getMessage().
') please check your settings.</h1>';
}
}
class NoFileOpenException extends Exception {
function __toString() {
return '<h1><b>ERROR:</b> could not open ('
.$this->getMessage().
') please check your settings.</h1>';
}
}
interface Reader {
function setFileName($fName);
function open();
function setBufferOffset($offset);
function bufferSize();
function isOffset();
function setPacketSize($size);
function read();
function isEOF();
function close();
function readAll();
}
class ProgressiveReader implements Reader {
private $fName;
private $fileHandler;
private $offset = 0;
private $packetSize = 0;
public function setFileName($fName) {
$this->fName = $fName;
if(!file_exists($this->fName)) {
throw new NoFileFoundException($this->fName);
}
}
public function open() {
try {
$this->fileHandler = fopen($this->fName, 'rb');
}
catch (Exception $e) {
throw new NoFileOpenException($this->fName);
}
fseek($this->fileHandler, $this->offset);
}
public function setBufferOffset($offset) {
$this->offset = $offset;
}
public function bufferSize() {
return filesize($this->fName) - (($this->offset > 0) ? ($this->offset + 1) : 0);
}
public function isOffset() {
if($this->offset === 0) {
return false;
}
return true;
}
public function setPacketSize($size) {
$this->packetSize = $size;
}
public function read() {
return fread($this->fileHandler, $this->packetSize);
}
public function isEOF() {
return feof($this->fileHandler);
}
public function close() {
if($this->fileHandler) {
fclose($this->fileHandler);
}
}
public function readAll() {
return fread($this->fileHandler, filesize($this->fName));
}
}
下面是單元測試:
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ProgressiveReader.php';
class ProgressiveReaderTest extends PHPUnit_Framework_TestCase {
protected $reader;
private $fp;
private $fname = "Test.txt";
protected function setUp() {
$this->createTestFile();
$this->reader = new ProgressiveReader();
}
protected function tearDown() {
$this->reader->close();
}
public function test_isValidFile() {
$this->reader->setFileName($this->fname);
}
public function test_isNotValidFile() {
try {
$this->reader->setFileName("nothing.tada");
}
catch (Exception $e) {
return;
}
$this->fail();
}
public function test_isFileOpen() {
$this->reader->setFileName($this->fname);
$this->reader->open();
}
public function test_couldNotOpenFile() {
$this->reader->setFileName($this->fname);
try {
$this->deleteTestFile();
$this->reader->open();
}
catch (Exception $e) {
return;
}
$this->fail();
}
public function test_bufferSizeZeroOffset() {
$this->reader->setFileName($this->fname);
$this->reader->open();
$this->assertEquals($this->reader->bufferSize(), 12);
}
public function test_bufferSizeTwoOffset() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(2);
$this->reader->open();
$this->assertEquals($this->reader->bufferSize(), 9);
}
public function test_readBuffer() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(0);
$this->reader->setPacketSize(1);
$this->reader->open();
$this->assertEquals($this->reader->read(), "T");
}
public function test_readBufferWithOffset() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(2);
$this->reader->setPacketSize(1);
$this->reader->open();
$this->assertEquals($this->reader->read(), "S");
}
public function test_readSuccesive() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(0);
$this->reader->setPacketSize(6);
$this->reader->open();
$this->assertEquals($this->reader->read(), "TEST1\n");
$this->assertEquals($this->reader->read(), "TEST2\n");
}
public function test_readEntireBuffer() {
$this->reader->setFileName($this->fname);
$this->reader->open();
$this->assertEquals($this->reader->readAll(), "TEST1\nTEST2\n");
}
public function test_isNotEOF() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(2);
$this->reader->setPacketSize(1);
$this->reader->open();
$this->assertFalse($this->reader->isEOF());
}
public function test_isEOF() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(0);
$this->reader->setPacketSize(15);
$this->reader->open();
$this->reader->read();
$this->assertTrue($this->reader->isEOF());
}
public function test_isOffset() {
$this->reader->setFileName($this->fname);
$this->reader->setBufferOffset(2);
$this->assertTrue($this->reader->isOffset());
}
public function test_isNotOffset() {
$this->reader->setFileName($this->fname);
$this->assertFalse($this->reader->isOffset());
}
private function createTestFile() {
$this->fp = fopen($this->fname, "wb");
fwrite($this->fp, "TEST1\n");
fwrite($this->fp, "TEST2\n");
flush();
fclose($this->fp);
}
private function deleteTestFile() {
if(file_exists($this->fname)) {
unlink($this->fname);
}
}
}
2
您可以直接連接到數據庫服務器嗎?
如果是這樣,我會考慮使用像SQLyog第三方程序來導入您的csv。
你也可以上傳文件,並使用mysql外殼直接導入數據:
LOAD DATA INFILE '/path/to/your_file.csv' INTO TABLE table_name FIELDS TERMINATED BY ',';
1
您的腳本可能花費的時間太長,它被終止。
您應該在php.ini中查找max_execution_time指令並將其設置爲適合您的值。
默認的max_execution_time設置爲30秒,所以你的腳本可能會被終止。
如果您還有腳本需要及時進行限制,您可以通過調用set_time_init()來單獨執行該腳本;
1
您是否嘗試過使用bash/shell(如果您在linux上)將您的csv導入到mysql中?你也可以使用ruby或者perl或者whatnot,因爲我認爲你應該使用它來代替php(或任何web應用程序)來導入文件。
2
此讀取整個CSV文件到一個數組
所有50000+行?
通過逐行讀取(fgets()),然後將每個(需要的)行添加到數組,從PHP開始讀取文件的所需塊;你可以用fgetcsv()獲得該行的數組。
編輯:我不知道確切的細節,但我覺得將所有內容讀入數據結構的成本比讀取我們需要的更多。
1
我會建議使用快速MySQL的LOAD DATA INFILE命令:
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
如果這不是一種選擇,你可能分裂CSV文件(假設訪問shell)。
0
呸!忽略這個答案。是重複的。見Scorchio上面提到的fgetcsv()。
相關問題
- 1. 在Java中讀取大型CSV文件
- 2. 讀取R中的大型csv文件
- 3. 從PHP中的大型CSV文件讀取多列
- 4. 閱讀大型csv文件
- 5. 閱讀大型CSV文件
- 6. 雙讀取大CSV文件
- 7. 在PHP中讀取CSV文件數據
- 8. 在php中讀取csv文件
- 9. PHP讀取從CSV文件
- 10. 閱讀PHP中的大csv文件,無法讀取
- 11. 在Flex中按行讀取大型CSV文件
- 12. 在C++中讀取大型CSV文件(〜4GB)
- 13. 在Python中讀取大型CSV文件熊貓
- 14. 用PHP讀取大型excel文件
- 15. 在vb6中讀取大型xml文件
- 16. 在SQLite中讀取大型SQL文件
- 17. 在R中讀取大型HDF5文件
- 18. 在PHP中逐行讀取大文件
- 19. 讀取R中的大型CSV文件時出錯
- 20. 將大型csv文件讀入R
- 21. 讀取大型XML文件
- 22. 閱讀CSV文件在PHP
- 23. NodeJS:讀取一個大的csv文件
- 24. 以大塊格式讀取CSV文件
- 25. OleDb - 讀取CSV文件的大問題
- 26. 從csv文件中讀取
- 27. 讀取.csv文件。
- 28. PHP排序大型csv文件
- 29. PHP有效地讀取csv文件
- 30. PHP逐行讀取CSV文件
你有沒有嘗試過像'ini_set(「max_execution_time」,0)''這樣的最大執行時間? – robjmills 2010-05-26 15:46:06
幾個問題: - 你如何將文件導入數據庫? - 您是在導入之前上傳文件還是實時讀取文件? – allnightgrocery 2010-05-26 15:48:49