public function writeAttempt()
    {
        $result = self::OK;

        if (empty($this->filepath)) {
            return self::ERROR;
        }

        /**
         * Open the file for writing only. If the file does not exist, it is created.
         * If it exists, it is not truncated (as opposed to 'w'). File pointer is moved to beginning.
         *
         * "c" is needed instead of "w", because if lock is not acquired, old version of the file is returned.
         * If "w" option is used, file is truncated no matter what, and empty file is returned.
         */
        $fp = fopen($this->filepath, 'c');

        if ($fp === false) {
            return self::ERROR_OPEN;
        }

        /**
         * File locking disabled?
         */
        if (empty($this->file_lock)) {
            ftruncate($fp, 0);
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }
            fclose($fp);

            return $result;
        }

        /**
         * File locking is enabled
         *
         * Try to acquire File Write Lock. If lock is not acquired read file and return old contents.
         *
         * Recommended is: LOCK_EX | LOCK_NB.
         * LOCK_EX to acquire an exclusive lock (writer).
         * LOCK_NB - prevents flock() from blocking while locking, so that others could still read
         * (but not write) while lock is active
         */
        if (flock($fp, $this->file_lock)) {
            /**
             * since "c" was used with fopen, file is not truncated. Truncate manually.
             */
            ftruncate($fp, 0);

            //write content
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }

            //release lock
            flock($fp, LOCK_UN);
        } else {
            /**
             * Lock was not granted.
             */

            $result = self::ERROR_LOCK;
        }

        fclose($fp);

        return $result;
    }
public function writeAttempt()
    {
        $result = self::OK;

        if (empty($this->filepath)) {
            return self::ERROR;
        }

        /**
         * Open the file for writing only. If the file does not exist, it is created.
         * If it exists, it is not truncated (as opposed to 'w'). File pointer is moved to beginning.
         *
         * "c" is needed instead of "w", because if lock is not acquired, old version of the file is returned.
         * If "w" option is used, file is truncated no matter what, and empty file is returned.
         */
        $fp = fopen($this->filepath, 'c');

        if ($fp === false) {
            return self::ERROR_OPEN;
        }

        /**
         * File locking disabled?
         */
        if (empty($this->file_lock)) {
            ftruncate($fp, 0);
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }
            fclose($fp);

            return $result;
        }

        /**
         * File locking is enabled
         *
         * Try to acquire File Write Lock. If lock is not acquired read file and return old contents.
         *
         * Recommended is: LOCK_EX | LOCK_NB.
         * LOCK_EX to acquire an exclusive lock (writer).
         * LOCK_NB - prevents flock() from blocking while locking, so that others could still read
         * (but not write) while lock is active
         */
        if (flock($fp, $this->file_lock)) {
            /**
             * since "c" was used with fopen, file is not truncated. Truncate manually.
             */
            ftruncate($fp, 0);

            //write content
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }

            //release lock
            flock($fp, LOCK_UN);
        } else {
            /**
             * Lock was not granted.
             */

            $result = self::ERROR_LOCK;
        }

        fclose($fp);

        return $result;
    }
public function writeAttempt ( ) { $ result = self :: OK ; if ( empty ( $ this -> filepath ) ) { return self :: ERROR ; } /**
         * Open the file for writing only. If the file does not exist, it is created.
         * If it exists, it is not truncated (as opposed to 'w'). File pointer is moved to beginning.
         *
         * "c" is needed instead of "w", because if lock is not acquired, old version of the file is returned.
         * If "w" option is used, file is truncated no matter what, and empty file is returned.
         */ $ fp = fopen ( $ this -> filepath , 'c' ) ; if ( $ fp === false ) { return self :: ERROR_OPEN ; } /**
         * File locking disabled?
         */ if ( empty ( $ this -> file_lock ) ) { ftruncate ( $ fp , 0 ) ; if ( fwrite ( $ fp , $ this -> content ) === false ) { $ result = self :: ERROR_WRITE ; } fclose ( $ fp ) ; return $ result ; } /**
         * File locking is enabled
         *
         * Try to acquire File Write Lock. If lock is not acquired read file and return old contents.
         *
         * Recommended is: LOCK_EX | LOCK_NB.
         * LOCK_EX to acquire an exclusive lock (writer).
         * LOCK_NB - prevents flock() from blocking while locking, so that others could still read
         * (but not write) while lock is active
         */ if ( flock ( $ fp , $ this -> file_lock ) ) { /**
             * since "c" was used with fopen, file is not truncated. Truncate manually.
             */ ftruncate ( $ fp , 0 ) ; //write content if ( fwrite ( $ fp , $ this -> content ) === false ) { $ result = self :: ERROR_WRITE ; } //release lock flock ( $ fp , LOCK_UN ) ; } else { /**
             * Lock was not granted.
             */ $ result = self :: ERROR_LOCK ; } fclose ( $ fp ) ; return $ result ; }
Attempt to write into the file. If lock is not acquired, file is not written.

@return bool
@throws \Exception
Attempt to write into the file . If lock is not acquired file is not written .