亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Storage::append() 中“Allowed memory size

Storage::append() 中“Allowed memory size

PHP
千巷貓影 2023-03-26 14:50:48
我用 Laravel 實現了一個代碼來處理塊上傳,如下所示。// if total size equals length of file we have gathered all patch filesif ($size == $length) {    // write patches to file    foreach ($patch as $filename) {        // get offset from filename        list($dir, $offset) = explode('.patch.', $filename, 2);        // read patch and close        $patch_contents = Storage::disk('tmp')->get($filename);        // apply patch        Storage::disk('tmp')->append($dir . $name, $patch_contents, "");    }    // remove patches    foreach ($patch as $filename) {        Storage::disk('tmp')->delete($filename);    }}問題是大文件會出現以下錯誤。"Allowed memory size of 134217728 bytes exhausted (tried to allocate 160000008 bytes)"我知道錯誤與 append 方法有關。我根據此鏈接中的解決方案解決了問題,如下所示。// if total size equals length of file we have gathered all patch filesif ($size == $length) {    $time_limit = ini_get('max_execution_time');    $memory_limit = ini_get('memory_limit');    set_time_limit(0);    ini_set('memory_limit', '-1');    // write patches to file    foreach ($patch as $filename) {        // get offset from filename        list($dir, $offset) = explode('.patch.', $filename, 2);        // read patch and close        $patch_contents = Storage::disk('tmp')->get($filename);        // apply patch        Storage::disk('tmp')->append($dir . $name, $patch_contents, "");    }    // remove patches    foreach ($patch as $filename) {        Storage::disk('tmp')->delete($filename);    }    set_time_limit($time_limit);    ini_set('memory_limit', $memory_limit);}但是我對這個解決方案沒有很好的感覺!我的問題是,首先,為什么append方法會出現這樣的錯誤呢?解決方案是否合適?另一方面,Laravel 對這個問題的解決方案是什么?
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

根本原因似乎在append的來源。

$this->put($path,?$this->get($path).$separator.$data);

這將獲取文件內容,然后連接數據并將文件放回原處。不知道為什么這樣做,但我猜這是因為所有存儲類型都不支持以追加模式打開文件,并且必須Storage實現CloudFilesystemContract這意味著它適用于云存儲(你通常不能“追加” " 到一個文件)。

深入挖掘可以發現 Laravel 的“驅動程序”由FlysystemStorage支持,并且其接口中不包含功能,因此 Laravel 只能使用提供的接口方法來實現一個功能。append

您始終可以推出自己的解決方案:

// if total size equals length of file we have gathered all patch files

if ($size == $length) {

? ? // write patches to file

? ? foreach ($patch as $filename) {

? ? ? ? // get offset from filename

? ? ? ? list($dir, $offset) = explode('.patch.', $filename, 2);


? ? ? ? // read patch and close

? ? ? ? $patch_contents = Storage::disk('tmp')->get($filename);


? ? ? ? // apply patch

? ? ? ? $fhandle = fopen($dir.$name, "a"); // You may need to adjust the filename?

? ? ? ? fwrite($fhandle, $patch_contents);

? ? ? ? fclose($fhandle);

? ? }


? ? // remove patches

? ? foreach ($patch as $filename) {

? ? ? ? Storage::disk('tmp')->delete($filename);

? ? }

}


查看完整回答
反對 回復 2023-03-26
  • 1 回答
  • 0 關注
  • 98 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號