Maison  >  Article  >  développement back-end  >  Comment éviter l'épuisement de la mémoire lors du traitement de fichiers volumineux en PHP à l'aide de File_get_contents ?

Comment éviter l'épuisement de la mémoire lors du traitement de fichiers volumineux en PHP à l'aide de File_get_contents ?

Patricia Arquette
Patricia Arquetteoriginal
2024-10-17 13:37:02370parcourir

How to Avoid Memory Exhaustion When Processing Large Files in PHP Using File_get_contents?

File Processing Using File_get_contents Encounters Memory Exhaustion

When working with large files in PHP, using the file_get_contents function to fetch the entire file contents into a variable can cause memory exhaustion errors. This is because the variable containing the file contents resides in memory, and for large files, the allocated memory limit can be exceeded.

To overcome this issue, a more efficient approach is to use file pointers and process the file in chunks. This way, only the current portion of the file is held in memory at any given time.

Here's a custom function that implements this chunked file processing:

<code class="php">function file_get_contents_chunked($file, $chunk_size, $callback)
{
    try {
        $handle = fopen($file, "r");
        $i = 0;
        while (!feof($handle)) {
            call_user_func_array($callback, [fread($handle, $chunk_size), &$handle, $i]);
            $i++;
        }

        fclose($handle);

        return true;
    } catch (Exception $e) {
        trigger_error("file_get_contents_chunked::" . $e->getMessage(), E_USER_NOTICE);
        return false;
    }
}</code>

To use this function, define a callback function to handle each chunk of data:

<code class="php">$success = file_get_contents_chunked("my/large/file", 4096, function($chunk, &$handle, $iteration) {
    // Perform file processing here
});</code>

Additionally, consider refactoring your regex operations to use native string functions like strpos, substr, trim, and explode. This can significantly improve performance when working with large files.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn