Write contents to a file

@param   string $file   The full file path
@param   string $buffer The buffer to write

@return  boolean  True on success

@since   2.0
@throws  FilesystemException
486115
public static function write($file, $buffer)
    {
        @set_time_limit((int) ini_get('max_execution_time'));

        // If the destination directory doesn't exist we need to create it
        if (!is_dir(dirname($file))) {
            Folder::create(dirname($file));
        }

        $file = Path::clean($file);

        return is_int(file_put_contents($file, $buffer)) ? true : false;
    }
831104
public static function write($file, $data, $lock = false) {
		return file_put_contents($file, $data, $lock ? LOCK_EX : 0);
	}
1170729
public function save($file, $content)
    {
        $dir = dirname($file);

        if (!is_dir($dir)) {
            mkdir($dir, 0777);
        }

        if (file_put_contents($file, $content)) {
            chmod($file, 0777);

            return true;
        } else {
            $message = "Cannot open cache file for writing: ";
            //			if (!Nip_Staging::instance()->isPublic()) {
            //				$message .= " [ ".$this->cache_file." ] ";
            //			}
            die($message);
        }
    }
507813
public function write($file, $content = "", $mode = "w+")
    {

        $stream = $this->getFileStream($file, $mode);

        //Write the contents
        fwrite($stream, $content);
        fclose($stream);

        return true;
    }
181469
private function write_to($file, $data)
  {
    $handler = fopen($file, "w");
    fwrite($handler, $data);
    fclose($handler);
  }
317861
private void writeFileByte(byte[] content, File file) throws IOException {

        FileOutputStream fOut = null;
        DataOutputStream dOut = null;
        try {
            // write the content to the file in server filesystem
            fOut = new FileOutputStream(file);
            dOut = new DataOutputStream(fOut);
            dOut.write(content);
            dOut.flush();
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (fOut != null) {
                    fOut.close();
                }
            } catch (IOException e) {
                // ignore
            }
            try {
                if (dOut != null) {
                    dOut.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
1000445
public function writePoFile(string $file): void
    {
        $source = $this->dumpString();
        $testName = file_exists($file) ? $file : dirname($file);
        $status = is_writable($testName);
        if ($status === true) {
            $status = file_put_contents($file, $source);
        }
        if (false === $status) {
            throw new FileNotWritableException($file);
        }
    }
933424
private function saveContent($file, $content)
    {
        if (file_put_contents($file, $content, LOCK_EX) === false) {
            throw new RuntimeException('Plugin Manager is not able to save ' . $file . '.'); // @codeCoverageIgnore
        }
    }
167334
public static function append(string $file = '', string $data = ''): bool
    {
        return static::write($file, $data, true);
    }
144755
public function save($file)
	{
		$handle = strcasecmp(substr($file, -3), '.gz') ? fopen($file, 'wb') : gzopen($file, 'wb');
		if (!$handle) {
			throw new Exception("ERROR: Cannot write file '$file'.");
		}
		$this->write($handle);
	}
696737
public function write($buffer)
    {
        $buffer = (string) $buffer;
        $bufLen = Binary::safeStrlen($buffer);

        $socket = $this->getSocket();
        $written = 0;

        while ($written < $bufLen) {
            // PHP's fwrite does not behave nice in regards to errors, so we wrap
            // it with a temporary error handler and treat every warning/notice as
            // a error
            $failed = false;
            $errorMessage = "Failed to write to socket";
            set_error_handler(function ($errno, $errstr) use (&$failed, &$errorMessage) {
                $failed = true;
                $errorMessage .= ": $errstr ($errno)";
            });
            $byteCount = fwrite($socket, Binary::safeSubstr($buffer, $written));
            restore_error_handler();

            if ($byteCount === 0 && defined('HHVM_VERSION')) {
                $failed = true;
            }

            if ($failed || $byteCount === false) {
                throw new \RuntimeException($errorMessage);
            }

            $written += $byteCount;
        }


        return $written;
    }
106477
public static function write(string $file = '', string $data = '', bool $doAppend = false): bool
    {
        $result = false;
        $file = static::normalizePath($file);
        if (static::verifyPath($file) && Directory::create(\dirname($file))) {
            $flags = $doAppend ? FILE_APPEND : 0;
            $flags |= static::$writeLocks ? LOCK_EX : 0;

            $result = @\file_put_contents($file, $data, $flags) !== false;
            @\chmod($file, static::$newFileMode);
        }
        return $result;
    }
1156017
public function afterScript(Buffer $buffer)
    {
        $buffer->write(sprintf('$z->pop(\'%s\');', $this->key));
        $buffer->write(sprintf('$z->pop(\'%s\');', $this->value));
        $buffer->indent(-1);
        $buffer->writeln('}');
    }
1217064
private function write( string $file, $data, $append ): bool
	{
		if( function_exists('error_clear_last') )
		{
			error_clear_last();
		}

		// check directory exists or create empty directory for file
		$path = dirname($file);
		if( ! is_dir($path) )
		{
			try
			{
				if( ! @ mkdir($path, 0755, true) ) throw new WriteException("Cannot create the '{$path}' directory");
			}
			catch( \ErrorException $e )
			{
				return $this->getError($e);
			}
		}

		// check directory is writable
		if( ! is_writable($path) )
		{
			return $this->getError(new WriteFileException($file));
		}

		// detect write mode
		// ignore append flag if file is not exists
		$mode = "wa+";
		if( $append )
		{
			if( file_exists( $file ) )
			{
				$mode = "w";
			}
			else
			{
				$append = false;
			}
		}

		try {
			$handle = @ fopen( $file, $mode );
			if( ! $handle )
			{
				throw new WriteFileException($file,"Cannot open the '{$file}' file for write");
			}

			$callable = $data instanceof \Closure;
			if( flock( $handle, LOCK_EX ) )
			{
				if( $callable )
				{
					do
					{
						$content = $data( $append );
						if( ! is_string($content) || strlen($content) < 1 )
						{
							break;
						}
						if( fwrite( $handle, $content ) === false )
						{
							throw new WriteFileException($file);
						}
					}
					while(true);
				}
				else if( fwrite( $handle, $data ) === false )
				{
					throw new WriteFileException($file);
				}

				fflush( $handle );
				flock(  $handle, LOCK_UN );
			}
			else
			{
				throw new WriteFileException($file);
			}
		}
		catch( \ErrorException $e )
		{
			if( isset($handle) && ! $append && file_exists($file) )
			{
				@ unlink($file);
			}
			return $this->getError($e);
		}
		finally
		{
			if( isset($handle) )
			{
				fclose($handle);
			}
		}

		if( $this instanceof Loggable )
		{
			$this->addLogDebug("The '{$file}' file is successfully " . ($append ? "updated" : "created"));
		}

		return true;
	}
159085
public function writeToCache(FileStorage $storage, $file_contents)
    {
        $file_path = strtolower($storage->file_path);
        $cache_location = $this->getCacheLocationForPath($file_path, true);
        $storage->hash = $this->getCacheHash($file_path, $file_contents);

        if ($this->config->use_igbinary) {
            file_put_contents($cache_location, igbinary_serialize($storage));
        } else {
            file_put_contents($cache_location, serialize($storage));
        }
    }
855871
public function send_file($file) {
        $this->googleoauth->setHeader("Content-Length: ". $file->get_filesize());
        $this->googleoauth->setHeader("Content-Type: ". $file->get_mimetype());
        $this->googleoauth->setHeader("Slug: ". $file->get_filename());

        $this->googleoauth->post(self::UPLOAD_LOCATION, $file->get_content());

        if ($this->googleoauth->info['http_code'] === 201) {
            return true;
        } else {
            return false;
        }
    }
139234
protected function writeToFile($file, $salts, Event $event)
    {
        try {
            if (copy(sprintf('%s.example', $file), $file)) {
                file_put_contents($file, implode($salts, "\n"), FILE_APPEND | LOCK_EX);
                return 0;
            }
        } catch (\Exception $e) {
            $event->getIO()->write('<error>An error occured while copying your .env file</error>');
            return 1;
        }
    }