Written by James McDonald

June 28, 2020

So recently my code editor has been putting a line through File because it is going to removed in CakePHP 5

So here is an example of code that uses the deprecated methods and the suggested SplFileObject

Using File

public function createTempFile($print_content, $print_settings = [])
    {
        if (isset($print_settings['temp_file'])) {
            $tempFile = $print_settings['temp_file'];
        } else {
            $tempFile = TMP . 'PrintTempFile.txt';
        }

        $print_file = new File($tempFile, true, 0644);

        $print_file->write($print_content);

        $print_file->close();
    }

Using SplFileObject

Note that to “close()” the file you assign the SplFileObject to null

Also there doesn’t appear to be an inbuilt chmod so you just have to use the PHP builtin

    public function createTempFile($print_content)
    {
        $tempFile = TMP . sprintf( 'PrintTempFile-%s.txt', $this->action);

        $print_file = new SplFileObject($tempFile, 'w');
        
        chmod ($print_file->getRealPath(), 0666);

        $print_file->fwrite($print_content);

        $print_file = null;
    }

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...