Need to split variable too large for PHP memory

Hi there folks!

I’ve got a portion of a script that takes the sql from a table and inserts it into a file. The issue I’m running into is that it sometimes exhausts PHP memory so I’d like to alter the function to split the $sql and insert it into file in stages so it won’t run into the issue. I have some points that I wonder about.

  1. Can a script compare the size of $sql with PHP memory allocation without me telling the script that that limit is? This script runs across a lot of different servers.

  2. How can I make sure that it splits at a line end so I don’t get a sql line corrupted because it gets split in the middle of a statement?

I have more questions but they depend on the answers to the two above so I’ll hold off on those for now.

Here’s my function:

     * Save SQL to file
     * @param string $sql
    protected function saveFile(&$sql) {
        if (!$sql) return false;

        try {

            if (!file_exists($this->backupDir)) {
                mkdir($this->backupDir, 0777, true);

            file_put_contents($this->backupDir.'/'.$this->backupFile, $sql, FILE_APPEND | LOCK_EX);

        } catch (Exception $e) {
            return false;

        return true;

Thanks for your time!

immediate impulse: throttle/paginate your SQL query to prevent this problem. Will also take care of the line-break requirement.

alternatively: use the database’s backup features, rather than using PHP to simulate it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.