Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read large files in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to read large files in PHP, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Measure success

The only way to confirm whether the improvements we have made to the code are effective is to measure a bad situation and then compare the measurements we have applied. In other words, we don't know if a solution is a solution unless we know to what extent, if any, the solution can help us.

We can focus on two indicators. The first is CPU usage. How fast or slow is the process we are going to deal with? The second is memory utilization. How much memory does script execution take? These are usually inversely proportional-which means that we can reduce memory usage at the expense of CPU usage and vice versa.

CPU and memory usage are important considerations in an asynchronous processing model, such as multiprocess or multithreaded PHP applications. In traditional PHP architectures, this is usually a hassle whenever the server limit is reached.

It is difficult to measure CPU usage within PHP. If you really care about this area, consider using commands similar to top in Ubuntu or macOS. For Windows, consider using the Linux subsystem so that you can use the top command in Ubuntu.

In this tutorial, we will measure memory usage. We'll take a look at how much memory is used by "traditional" scripts. We will also implement some optimization strategies and measure them. Finally, I hope you can make a reasonable choice.

The following is the method we use to view memory usage:

/ / the formatBytes method is based on the php.net document memory_get_peak_usage (); function formatBytes ($bytes, $precision = 2) {$units = array ("b", "kb", "mb", "gb", "tb"); $bytes = max ($bytes, 0); $pow = floor (($bytes? Log ($bytes): 0) / log (1024); $pow = min ($pow, count ($units)-1); $bytes / = (1 open ($filename, ZipArchive::CREATE); $zip- > addFromString ("shakespeare.txt", file_get_contents ("shakespeare.txt")); $zip- > close (); require "memory.php"

This code is neat, but uses a total of about 10.75 MB of memory. We can use filters to optimize

/ / from filters-2.php$handle1 = fopen ("php://filter/zlib.deflate/resource=shakespeare.txt", "r"); $handle2 = fopen ("filters-2.deflated", "w"); stream_copy_to_stream ($handle1, $handle2); fclose ($handle1); fclose ($handle2); require "memory.php"

Here, we can see the php:///filter/zlib.deflate filter, which reads and compresses the contents of the resource. We can then transfer the compressed data to another file through a pipeline. Only 896KB memory is used.

Although the format is different, or there are many other benefits of using zip to compress files. However, you have to consider: if you choose another format, you can save 12 times the memory, will you be tempted?

To extract the data, simply pass through another zlib filter:

/ / from filters-2.phpfile_get_contents ("php://filter/zlib.inflate/resource=filters-2.deflated"); Custom flow

Fopen and file_get_contents have their own default set of options, but they are fully customizable. To define them, we need to create a new flow context

/ / from creating-contexts-1.php$data = join ("&", ["twitter=assertchris",]); $headers = join ("\ r\ n", ["Content-type: application/x-www-form-urlencoded", "Content-length:". Strlen ($data),]; $options = ["http" = > ["method" = > "POST", "header" = > $headers, "content" = > $data,],]; $context = stream_content_create ($options); $handle = fopen ("https://example.com/register"," r ", false, $context); $response = stream_get_contents ($handle); fclose ($handle)

In this example, we try to send a POST request to API. The API endpoint is secure, but we still use the http context attribute (which can be used for http or https). We set some headers and opened the file handle to API. We can open the handle as read-only and the context is responsible for writing.

There are a lot of custom content, if you want to know more information, you can check the corresponding documentation.

Create custom protocols and filters

Before summarizing, let's talk about creating a custom protocol. If you look at the documentation, you can find a sample class:.

Protocol {public resource $context Public _ construct (void) public _ destruct (void) public bool dir_closedir (void) public bool dir_opendir (string $path, int $options) public string dir_readdir (void) public bool dir_rewinddir (void) public bool mkdir (string $path, int $mode, int $options) public bool rename (string $path_from, string $path_to) public bool rmdir (string $path Int $options) public resource stream_cast (int $cast_as) public void stream_close (void) public bool stream_eof (void) public bool stream_flush (void) public bool stream_lock (int $operation) public bool stream_metadata (string $path, int $option, mixed $value) public bool stream_open (string $path, string $mode, int $options, string & $opened_path) public string stream_read (int $count) public bool stream_seek (int $offset Int $whence = SEEK_SET) public bool stream_set_option (int $option, int $arg1, int $arg2) public array stream_stat (void) public int stream_tell (void) public bool stream_truncate (int $new_size) public int stream_write (string $data) public bool unlink (string $path) public array url_stat (string $path, int $flags)

We're not going to implement one of them because I think it deserves its own tutorials. There's a lot of work to do. But once the work is done, we can easily register the stream wrapper:

If (in_array ("highlight-names", stream_get_wrappers ()) {stream_wrapper_unregister ("highlight-names");} stream_wrapper_register ("highlight-names", "HighlightNamesProtocol"); $highlighted = file_get_contents ("highlight-names://story.txt")

Similarly, you can create a custom flow filter. The document has a sample filter class:

Filter {public $filtername; public $params public int filter (resource $in, resource $out, int & $consumed, bool $closing) public void onClose (void) public bool onCreate (void)}

Can be easily registered

$handle = fopen ("story.txt", "w +"); stream_filter_append ($handle, "highlight-names", STREAM_FILTER_READ)

The highlight-names needs to match the filtername property of the new filter class. You can also use custom filters in php:///filter/highligh-names/resource=story.txt strings. Defining a filter is much easier than defining a protocol. One reason is that the protocol needs to handle directory operations, while the filter only needs to process each block of data.

If you like, I strongly recommend that you try to create custom protocols and filters. If you can apply filters to stream_copy_to_stream operations, even if you work with annoying large files, your application will use almost no memory. Imagine writing resized image filters or encrypted application filters.

If you like, I strongly recommend that you try to create custom protocols and filters. If you can apply filters to stream_copy_to_stream operations, even if you deal with annoying large files, your application uses almost no memory. Imagine writing resize-image filters and encrypt-for-application filters.

On how to read large files in PHP to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report