2013-01-18 37 views
1

我讀文件的CSV值:約100MB如何修復致命錯誤:內存(分配)(試圖分配字節)在PHP

//$mypath . '/' . $filename <=> ../abc.csv 
$val = file_get_contents($mypath . '/' . $filename);          

$escaped = pg_escape_bytea($val); 

$model->addFileImport($tmp, $data['email'], $escaped); 

我的文件IA。 在php.ini中設置: memory_limit的= 128M

但它仍然顯示在行errort :Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 133120 bytes) in...$val = file_get_contents($mypath . '/' . $filename);

我已經通過固定加ini_set('memory_limit', '-1');

//$mypath . '/' . $filename <=> ../abc.csv 
ini_set('memory_limit', '-1'); 
$val = file_get_contents($mypath . '/' . $filename);          

$escaped = pg_escape_bytea($val); 

$model->addFileImport($tmp, $data['email'], $escaped); 

但它顯示錯誤:

Fatal error: Out of memory (allocated 230686720) (tried to allocate 657099991 bytes) in C:\wamp\www\joomlandk\components\com_servicemanager\views\i0701\view.html.php on line 112

在第$escaped = pg_escape_bytea($val);

爲什麼?如何解決這個錯誤?

+0

你耗盡內存,據我可以告訴物理內存。 657099991字節是626.659兆字節。最好的選擇是[使用更大的交換文件](https://www.google.com/search?q=increase+size+of+swap+file)。 –

回答

-1

嘗試添加:

ini_set('memory_limit', '700M'); 
+1

它顯示錯誤:致命錯誤:在第114行上的C:\ wamp \ www \ joomlandk \ components \ com_servicemanager \ views \ i0701 \ view.html.php中允許的內存大小爲734003200個字節已耗盡(試圖分配657099991個字節) val = file_get_contents($ mypath。'/'。$ filename); – mum

+0

他已經嘗試過-1,所以這沒有幫助 – Gordon

1

根據the doc

pg_escape_bytea() escapes string for bytea datatype. It returns escaped string.

When you SELECT a bytea type, PostgreSQL returns octal byte values prefixed with '\' (e.g. \032). Users are supposed to convert back to binary format manually.

意味着單個輸入字節給出的4個字節,即4倍的初始大小

你需要很多的RAM處理你的文件(也許你的系統不能分配那麼多的內存 - 即使沒有PHP限制)。解決方案是從40MB塊處理它,例如使用fread()fwrite()函數進行處理。

$val = file_get_contents($mypath . '/' . $filename); 

將需要100MB - 因此下一行需要400 MB,總共500MB。你需要閱讀*的file_get_contents少*,例如在時間FREAD(代替的file_get_contents)

  • 過程與20MB讀取只有20(或40)MB

    • 讀20MB的文件* pg_escape_bytea *(總100MB)
    • 重複上述過程,直到該文件被完全處理
  • +0

    但它在行顯示錯誤:$ escaped = pg_escape_bytea($ val); 我必須將字符串轉換爲bytea插入到數據庫。 – mum

    +0

    是的,因爲第一行* file_get_contents *需要100MB,那麼* pg_escape_bytea *行需要4次,即400MB。總共500MB ...這就是爲什麼錯誤出現 - 如果你只能* file_get_contents *(比如說)25MB,那麼總數只有125MB。查看編輯 –

    +0

    我可以爲web服務器設置memeory嗎? – mum

    相關問題