219mze 发表于 2024-10-4 19:54:02

怎么样在不引起服务器宕机的状况下,用 PHP 读取大文件


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRKicFqj21ETM6BSCXJgEUU7laNgFUjThHg934wurGgpeNInibLArLzib1doPnboqcdt3XjekFmV7Ejhibg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">协作翻译</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">原文</strong>:How to Read Big Files with PHP (Without Killing Your Server)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">链接</strong>:https://www.sitepoint.com/performant-reading-big-files-php/</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">译者</strong>:Tocy,&nbsp;Tony,&nbsp;南宫冰郁,&nbsp;Tot_ziens</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">做为</span>PHP<span style="color: black;">研发</span>人员,<span style="color: black;">咱们</span>并不经常需要担心内存管理。PHP 引擎在<span style="color: black;">咱们</span><span style="color: black;">背面</span>做了很好的清理工作,短期执行上下文的 Web 服务器模型<span style="color: black;">寓意</span>着即使是最潦草的代码<span style="color: black;">亦</span>不会<span style="color: black;">导致</span>持久的影响。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">很少<span style="color: black;">状况</span>下<span style="color: black;">咱们</span>可能需要走出这个舒适的<span style="color: black;">地区</span> ——<span style="color: black;">例如</span>当<span style="color: black;">咱们</span>试图在一个大型项目上运行 Composer 来创建我们<span style="color: black;">能够</span>创建的最小的 VPS 时,<span style="color: black;">或</span>当<span style="color: black;">咱们</span>需要在一个<span style="color: black;">一样</span>小的服务器上读取大文件时。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZ523oiaC70aMCJ8tpwCEJKDS0aLdGIkyny9jFuawKrty71HAOFZUiaVZg/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">后面的问题<span style="color: black;">便是</span><span style="color: black;">咱们</span>将在本教程中深入探讨的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在&nbsp;GitHub(<span style="color: black;">https://github.com/sitepoint-editors/sitepoint-performant-reading-of-big-files-in-php</span>) 上<span style="color: black;">能够</span>找到本教程的源码。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">衡量成功的标准</span></strong></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">保证</span><span style="color: black;">咱们</span>对代码有改进的<span style="color: black;">独一</span><span style="color: black;">办法</span>是测试一个<span style="color: black;">欠好</span>的<span style="color: black;">状况</span>,<span style="color: black;">而后</span>将<span style="color: black;">咱们</span>修复之后的<span style="color: black;">测绘</span>与另一个进行比较。换句话说,除非<span style="color: black;">咱们</span><span style="color: black;">晓得</span>“<span style="color: black;">处理</span><span style="color: black;">方法</span>”对<span style="color: black;">咱们</span>有多大的<span style="color: black;">帮忙</span>(<span style="color: black;">倘若</span>有的话),否则<span style="color: black;">咱们</span>不<span style="color: black;">晓得</span>它<span style="color: black;">是不是</span>真的是一个<span style="color: black;">处理</span><span style="color: black;">方法</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>有两个<span style="color: black;">咱们</span><span style="color: black;">能够</span>关系的衡量标准。<span style="color: black;">首要</span>是CPU<span style="color: black;">运用</span>率。<span style="color: black;">咱们</span>要处理的进程有多快或多慢?第二是内存<span style="color: black;">运用</span><span style="color: black;">状况</span>。脚本执行时需要多少内存?这两个<span style="color: black;">一般</span>是成反比的 - 这<span style="color: black;">寓意</span>着<span style="color: black;">咱们</span><span style="color: black;">能够</span>以CPU<span style="color: black;">运用</span>率为代价来降低内存<span style="color: black;">运用</span>,反之<span style="color: black;">也</span>然。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在一个异步执行模型(如多进程或多线程的PHP应用程序)中,CPU和内存的<span style="color: black;">运用</span>率是很重要的考量<span style="color: black;">原因</span>。在传统的PHP架构中,当任何一个值达到服务器的极限时,这些<span style="color: black;">一般</span>都会<span style="color: black;">作为</span>问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">测绘</span>PHP内的CPU<span style="color: black;">运用</span>率是不切<span style="color: black;">实质</span>的。<span style="color: black;">倘若</span>这是你要关注的<span style="color: black;">行业</span>,请<span style="color: black;">思虑</span>在Ubuntu或MacOS上<span style="color: black;">运用</span>类似top的工具。<span style="color: black;">针对</span>Windows,请<span style="color: black;">思虑</span><span style="color: black;">运用</span>Linux子系统,以便在Ubuntu中<span style="color: black;">运用</span>top。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了本教程的目的,<span style="color: black;">咱们</span>将<span style="color: black;">测绘</span>内存<span style="color: black;">运用</span><span style="color: black;">状况</span>。<span style="color: black;">咱们</span>将<span style="color: black;">瞧瞧</span>在“传统”的脚本中<span style="color: black;">运用</span>了多少内存。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>将执行<span style="color: black;">有些</span>优化策略并对其进行度量。最后,我<span style="color: black;">期盼</span>你能够做出一个有经验的<span style="color: black;">选取</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>查看内存<span style="color: black;">运用</span>多少的<span style="color: black;">办法</span>是:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZTU4TTH0l4Mb84DBhmAhrsBWbqjejRVZhzQuv2b2AqNQORo3xRcibEZA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>将在脚本的最后<span style="color: black;">运用</span>这些函数,以便<span style="color: black;">咱们</span>能够看到哪个脚本一次<span style="color: black;">运用</span>最大的内存。</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">咱们</span>的<span style="color: black;">选取</span>是什么?</span></strong></span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>有<span style="color: black;">非常多</span><span style="color: black;">办法</span><span style="color: black;">能够</span>有效地读取文件。<span style="color: black;">然则</span><span style="color: black;">亦</span>有两种<span style="color: black;">咱们</span>可能<span style="color: black;">运用</span>它们的情况。<span style="color: black;">咱们</span>想要<span style="color: black;">同期</span>读取和处理所有数据,输出处理过的数据或<span style="color: black;">按照</span><span style="color: black;">咱们</span>所读取的内容执行其他操作。<span style="color: black;">咱们</span><span style="color: black;">亦</span>可能想要转换一个数据流,而不需要真正<span style="color: black;">拜访</span>的数据。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">让<span style="color: black;">咱们</span>设想一下,<span style="color: black;">针对</span><span style="color: black;">第1</span>种<span style="color: black;">状况</span>,<span style="color: black;">咱们</span><span style="color: black;">期盼</span>读取一个文件,并且每10,000行创建一个独立排队的处理作业。<span style="color: black;">咱们</span>需要在内存中<span style="color: black;">保存</span><span style="color: black;">最少</span>10000行,并将它们传递给排队的工作管理器(无论采取何种形式)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>第二种<span style="color: black;">状况</span>,<span style="color: black;">咱们</span>假设<span style="color: black;">咱们</span>想要压缩一个<span style="color: black;">尤其</span>大的API响应的内容。<span style="color: black;">咱们</span>不在乎它的内容是什么,但<span style="color: black;">咱们</span>需要<span style="color: black;">保证</span>它是以压缩形式备份的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这两种<span style="color: black;">状况</span>下,<span style="color: black;">倘若</span><span style="color: black;">咱们</span>需要读取大文件,<span style="color: black;">首要</span>,<span style="color: black;">咱们</span>需要<span style="color: black;">晓得</span>数据是什么。第二,<span style="color: black;">咱们</span>并不在乎数据是什么。让<span style="color: black;">咱们</span>来探索这些<span style="color: black;">选取</span>吧...</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">逐行读取文件</span></strong></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有许多操作文件的函数,<span style="color: black;">咱们</span>把部分结合到一个简单的文件阅读器中(封装为一个<span style="color: black;">办法</span>):</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZ1PA0SlIHPZPhncDcr3s8aU2IrhzKzKnIQzqPCoxebTpzMyfAembzkg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZIRUibmjzGLA1OVriaUYgSNHB7DyXlTTRUYdblHicaCva79dpw9mAAQA7w/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>读取一个文本文件为莎士比亚全集。文件<span style="color: black;">体积</span>为5.5MB,内存占用峰值为12.8MB。<span style="color: black;">此刻</span>让<span style="color: black;">咱们</span>用一个生成器来读取每一行:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZhUG39fIiaQ0coHxFBHPTYib4SKrQ9BzSNLiadIZUrJRXHrY1jSNQgjzPA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">文本文件<span style="color: black;">体积</span>不变,但内存<span style="color: black;">运用</span>峰值只是393KB。即使<span style="color: black;">咱们</span>能把读取到的数据做<span style="color: black;">有些</span>事情<span style="color: black;">亦</span>并不<span style="color: black;">寓意</span>着什么。<span style="color: black;">亦</span>许<span style="color: black;">咱们</span><span style="color: black;">能够</span>在看到两条空白时把文档分割成块,像<span style="color: black;">这般</span>:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZqibmuY8Yoggz089mowuhg4TfOgeU3ib6A9JMJP369FdjZ51SR8MTOHjQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">猜到<span style="color: black;">咱们</span><span style="color: black;">运用</span>了多少内存吗?<span style="color: black;">咱们</span>把文档分割为1216块,仍然只<span style="color: black;">运用</span>了459KB的内存,这<span style="color: black;">是不是</span>让你惊讶?<span style="color: black;">思虑</span>到生成器的性质,<span style="color: black;">咱们</span><span style="color: black;">运用</span>的最多内存是<span style="color: black;">运用</span>在迭代中<span style="color: black;">咱们</span>需要存储的最大文本块。在本例中,最大的块为101985字符。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我<span style="color: black;">已然</span>撰写了</span><span style="color: black;">运用</span>生成器提示性能<span style="color: black;">和</span>Nikita Popov的迭代器库<span style="color: black;">,<span style="color: black;">倘若</span>你感兴趣就去<span style="color: black;">瞧瞧</span>吧!</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">生成器还有其它用途,<span style="color: black;">然则</span>最<span style="color: black;">显著</span>的好处<span style="color: black;">便是</span>高性能读取大文件。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>需要处理这些数据,生成器可能是最好的<span style="color: black;">办法</span>。</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">管道间的文件</strong></span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">咱们</span>不需要处理数据的<span style="color: black;">状况</span>下,<span style="color: black;">咱们</span><span style="color: black;">能够</span>把文件数据传递到另一个文件。<span style="color: black;">一般</span>被<span style="color: black;">叫作</span>为管道(大概是<span style="color: black;">由于</span><span style="color: black;">咱们</span>看不到除了两端的管子里面,当然,它<span style="color: black;">亦</span>是不透明的),<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span><span style="color: black;">运用</span>流<span style="color: black;">办法</span>实现。让<span style="color: black;">咱们</span>先写一个脚本从一个文件传到另一个文件。<span style="color: black;">这般</span><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">测绘</span>内存的占用<span style="color: black;">状况</span>:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZxAfX0PP9MGMEzeQ4k1ial6l39CIysRH0ib4xpIsBQBN1DkP2Cqhe4Gcw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不出所料,这个脚本<span style="color: black;">运用</span><span style="color: black;">更加多</span>的内存来进行文本文件复制。这是<span style="color: black;">由于</span>它读取(和<span style="color: black;">保存</span>)文件内容在内存中,直到它被写到新文件中。<span style="color: black;">针对</span>小文件这种<span style="color: black;">办法</span><span style="color: black;">亦</span>许没问题。当为更大的文件时,就捉襟见肘了…</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">让<span style="color: black;">咱们</span>尝试用流(管道)来传送一个文件到另一个:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZxaIibQ705KUcROU9FD3sjheWyfM3G7uM2Ow2WgvQCbSBT5dzEQLZFqw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这段代码稍微有点陌生。<span style="color: black;">咱们</span>打开了两文件的句柄,<span style="color: black;">第1</span>个是只读模式,第二个是只写模式,<span style="color: black;">而后</span><span style="color: black;">咱们</span>从<span style="color: black;">第1</span>个复制到第二个中。最后<span style="color: black;">咱们</span>关闭了它,<span style="color: black;">亦</span>许使你惊讶,内存只占用了393KB。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这似乎很<span style="color: black;">熟练</span>。像代码生成器在存储它读到的每一行代码?那是<span style="color: black;">由于</span>第二个参数fgets规定了每行读多少个字节(默认值是-1<span style="color: black;">或</span>直到下一<span style="color: black;">行径</span>止)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第三个参数stream_copy_to_stream和第二个参数是同一类参数(默认值相同),stream_copy_to_stream一次从一个数据流里读一行,<span style="color: black;">同期</span>写到另一个数据流里。它跳过生成器<span style="color: black;">仅有</span>一个值的部分(<span style="color: black;">由于</span><span style="color: black;">咱们</span>不需要这个值)。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这篇<span style="color: black;">文案</span><span style="color: black;">针对</span><span style="color: black;">咱们</span><span style="color: black;">来讲</span>可能是没用的,<span style="color: black;">因此</span>让<span style="color: black;">咱们</span>想<span style="color: black;">有些</span><span style="color: black;">咱们</span>可能会用到的例子。假设<span style="color: black;">咱们</span>想从<span style="color: black;">咱们</span>的CDN中输出一张<span style="color: black;">照片</span>,<span style="color: black;">做为</span>一种重定向的路由应用程序。<span style="color: black;">咱们</span><span style="color: black;">能够</span>参照下边的代码来实现它:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZ1WltzKhKTRq044NQ5Ec7VsAiaPKrPxlWrouaFwEyaqgtqGsTmFq4ebA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">设想一下,一个路由应用程序让<span style="color: black;">咱们</span>看到这段代码。<span style="color: black;">然则</span>,<span style="color: black;">咱们</span>想从CDN获取一个文件,而不是从本地的文件系统获取。<span style="color: black;">咱们</span><span style="color: black;">能够</span>用<span style="color: black;">有些</span>其他的东西来更好的替换file_get_contents(就像</span>Guzzle<span style="color: black;">),即使在引擎内部它们几乎是<span style="color: black;">同样</span>的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">照片</span>的内存大概有581K。<span style="color: black;">此刻</span>,让<span style="color: black;">咱们</span>来试试这个</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZCHFkFAouNibrbj2Lt0RTZxucZAUKfVPQGIRF96TZttfibdNXxNslbhuw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">内存<span style="color: black;">运用</span><span style="color: black;">显著</span>变少(大概400K),<span style="color: black;">然则</span>结果是<span style="color: black;">同样</span>的。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>不关注内存信息,<span style="color: black;">咱们</span>依旧<span style="color: black;">能够</span>用标准模式输出。<span style="color: black;">实质</span>上,PHP<span style="color: black;">供给</span>了一个简单的方式来完成:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZPatCe3RSpdduORXojeMVEHWT6fJdO8O1BDrlmos6mBvRTR9cCavneA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">其它流</strong></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">还有其它<span style="color: black;">有些</span>流,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span>管道来写入和读取(或只读取/只写入):</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://stdin&nbsp;(只读)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://stderr&nbsp;(只写, 如php://stdout)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://input&nbsp;(只读) 这使<span style="color: black;">咱们</span>能够<span style="color: black;">拜访</span>原始请求体</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://output&nbsp;(只写) 让<span style="color: black;">咱们</span>写入输出缓冲区</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">php://memory 和 php://temp&nbsp;(读-写) 是<span style="color: black;">咱们</span><span style="color: black;">能够</span>临时存储数据的<span style="color: black;">地区</span>。 <span style="color: black;">区别</span>之处在于一旦它变得足够大 php://temp&nbsp;会将数据存储在文件系统中,而 php://memory&nbsp;将<span style="color: black;">始终</span>持存储在内存中直到资源耗尽。</span></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">过滤器</span></strong></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">还有一个<span style="color: black;">咱们</span><span style="color: black;">能够</span>在stream上<span style="color: black;">运用</span>的技巧,<span style="color: black;">叫作</span>为过滤器。它们是一种中间的<span style="color: black;">过程</span>,<span style="color: black;">供给</span>对stream数据的<span style="color: black;">有些</span><span style="color: black;">掌控</span>,但不把<span style="color: black;">她们</span>暴露给<span style="color: black;">咱们</span>。想象一下,<span style="color: black;">咱们</span>会<span style="color: black;">运用</span>Zip扩展名来压缩<span style="color: black;">咱们</span>的shakespeare.txt文件。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZZ241r1ztpbZtNtNbOEm1rupFzlT6r5aYhaX84P5icj5057hZpswdhzw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是一小段整洁的代码,但它<span style="color: black;">测绘</span>内存占用在10.75MB<span style="color: black;">上下</span>。<span style="color: black;">运用</span>过滤器的话,<span style="color: black;">咱们</span><span style="color: black;">能够</span>减少内存:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZAOOxsHwqmN9OZTl6bCBVIlibmnC4qQdyfL1cOuQkEknatabqkiaIO1Pw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此处,<span style="color: black;">咱们</span><span style="color: black;">能够</span>看到名为php://filter/zlib.deflate的过滤器,它读取并压缩资源的内容。<span style="color: black;">咱们</span><span style="color: black;">能够</span>在之后将压缩数据导出到另一个文件中。这仅<span style="color: black;">运用</span>了896KB.</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">我<span style="color: black;">晓得</span>这是不<span style="color: black;">同样</span>的格式,<span style="color: black;">或</span>制作zip存档是有好处的。你不得不<span style="color: black;">可疑</span>:<span style="color: black;">倘若</span>你<span style="color: black;">能够</span><span style="color: black;">选取</span><span style="color: black;">区别</span>的格式并节省约12倍的内存,<span style="color: black;">为何</span>不选呢?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为<span style="color: black;">认识</span>压此数据,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span>执行另一个zlib filter将压缩后的数据还原:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZwDHqGh3KJjYgRcdqgewXgIVaxtWYyiamd5qFNen0AjO8nBhbiceZBpQw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Streams have been extensively covered in Stream在“</span>理解PHP中的流<span style="color: black;">”和“</span>U<span style="color: black;">有效</span><span style="color: black;">运用</span>PHP中的流</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">”中<span style="color: black;">已然</span>被全面介绍了。<span style="color: black;">倘若</span>你<span style="color: black;">爱好</span>一个完全<span style="color: black;">区别</span>的视角,<span style="color: black;">能够</span>阅读一下。</p>

    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">定制流</span></strong></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">fopen和file_get_contents有它们自己的一套默认选项,<span style="color: black;">然则</span>这些都是完全可定制的。为了定义它们,<span style="color: black;">咱们</span>需要创建一个新的流上下文:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZvegc7R814ic1WbicZiaPXmABoNc7YYW7HYjklgUjVXAm2VtlibtibyVDRhQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个例子中,<span style="color: black;">咱们</span>正在尝试向API发出POST请求。 API终端是安全的,但<span style="color: black;">咱们</span>仍然需要<span style="color: black;">运用</span>http上下文属性(用于http和https)。<span style="color: black;">咱们</span>设置<span style="color: black;">有些</span><span style="color: black;">信息</span>头参数,并打开一个文件句柄到API。<span style="color: black;">因为</span>上下文处理写操作,<span style="color: black;">咱们</span><span style="color: black;">能够</span>将句柄打开为只读。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">查看</span>文档<span style="color: black;"><span style="color: black;">认识</span><span style="color: black;">更加多</span>:<span style="color: black;">https://php.net/function.stream-context-create</span></span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">制定自定义协议和过滤器</span></strong></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">咱们</span>结束之前,让<span style="color: black;">咱们</span>谈谈制定自定义协议。 <span style="color: black;">倘若</span>你查看</span>文档<span style="color: black;">,你<span style="color: black;">能够</span>找到一个示例类来实现:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZsdmkjoClia4qBZwCWh09xg2EIZjYKbvpGJlAT5gia2yW4iatCVF3giaEGQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>不打算实现其中的一个,<span style="color: black;">由于</span>我认为它应该有自己的教程。<span style="color: black;">这儿</span>有很多工作需要完成。<span style="color: black;">然则</span>一旦这个工作完成,<span style="color: black;">咱们</span><span style="color: black;">能够</span>很容易地注册<span style="color: black;">咱们</span>的流包装:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZ49BXng0yTicctLHm8d6UoicqH80966DZjrovia3ACVENCEhDFVOpMywAg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">一样</span>,<span style="color: black;">亦</span><span style="color: black;">能够</span>创建自定义流过滤器。该</span>文档<span style="color: black;">有一个示例过滤器类:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZ4BL19iaBaj5V2ibBy9ooeohWqibCNjhqLbxO8LgFCyed7ibQHW7V2jdzCQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这<span style="color: black;">能够</span>很容易地注册:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/dkwuWwLoRK83Qyo9p3cJlwgYpWibhWbiaZsgc0mMZNn7W4MFH1gfX5O0XZFkyXqv7RicicYpSeiacj1ibsiaYUCj6xHqg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">明显</span><span style="color: black;">表示</span>名<span style="color: black;">叫作</span>需要匹配新的筛选器类的filtername属性。<span style="color: black;">亦</span><span style="color: black;">能够</span>在php://filter/highligh-names/resource=story.txt字符串中<span style="color: black;">运用</span>自定义过滤器。定义过滤器比定义协议要容易得多。<span style="color: black;">由于</span>协议需要处理目录操作,而过滤器只需处理<span style="color: black;">每一个</span>数据块。</p><span style="color: black;">倘若</span>你有这个想法,我<span style="color: black;">剧烈</span><span style="color: black;">意见</span>你尝试创建自定义协议和过滤器。<span style="color: black;">倘若</span>你<span style="color: black;">能够</span>将过滤器应用于stream_copy_to_streamoperations,<span style="color: black;">那样</span>即使在<span style="color: black;">运用</span>大容量文件时,你的应用程序<span style="color: black;">亦</span><span style="color: black;">能够</span>在<span style="color: black;">无</span>内存的<span style="color: black;">状况</span>下<span style="color: black;">运用</span>。想象一下,编写一个<span style="color: black;">调节</span><span style="color: black;">体积</span>的图像过滤器或加密的应用程序过滤器。
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">总结</span></strong></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虽然这不是<span style="color: black;">咱们</span>经常遇到的问题,但在处理大文件时很容易搞砸。在异步应用程序中,当<span style="color: black;">咱们</span>不<span style="color: black;">重视</span>小心<span style="color: black;">运用</span>内存的话,很容易<span style="color: black;">引起</span><span style="color: black;">全部</span>服务器宕机。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本教程<span style="color: black;">期盼</span>向你介绍<span style="color: black;">有些</span>新的想法(<span style="color: black;">或</span>让你重新认识<span style="color: black;">她们</span>),以便你可以<span style="color: black;">更加多</span>地<span style="color: black;">思虑</span><span style="color: black;">怎样</span><span style="color: black;">有效</span>地读取和写入大型文件。当<span style="color: black;">咱们</span><span style="color: black;">起始</span><span style="color: black;">熟练</span>流程和生成器,并停止<span style="color: black;">运用</span>像file_get_contents<span style="color: black;">这般</span>的函数时,<span style="color: black;">咱们</span>的应用程序中就会减少错误的类别,这看起来是很好。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">举荐</span>阅读</span>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">2018 最具就业前景的 7 大编程语言,前三无悬念?</a></span></h2>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">要火!Python 纳入高考科目;PHP、JS 等主流编</a>程语言爆安全漏洞</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">怎样</span>愉快的<span style="color: black;">运用</span> MQ - 详述<span style="color: black;">各样</span>功能场景</a></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">Java 10 新特性解密,引入类型推断机制</a></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">必须</span> SQL <span style="color: black;">查找</span>优化技巧,<span style="color: black;">提高</span>网站<span style="color: black;">拜访</span>速度</a></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">点击<strong style="color: blue;">“阅读原文”</strong>查看<span style="color: black;">更加多</span>精彩内容</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>




j8typz 发表于 2024-10-12 08:13:31

系统提示我验证码错误1500次 \~゛,

j8typz 发表于 2024-10-13 03:02:51

你的话语如春风拂面,让我心生暖意。

qzmjef 发表于 7 天前

说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。
页: [1]
查看完整版本: 怎么样在不引起服务器宕机的状况下,用 PHP 读取大文件