Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | is_dir( ) Function
Next article icon

PHP | fwrite( ) Function

Last Updated : 19 Jun, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The fwrite() function in PHP is an inbuilt function which is used to write to an open file. The fwrite() function stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file, string and the length which has to be written are sent as parameters to the fwrite() function and it returns the number of bytes written on success, or FALSE on failure.

Syntax:

fwrite(file, string, length)

Parameters: The fwrite() function in PHP accepts three parameters.

  1. file : It is a mandatory parameter which specifies the file.
  2. string : It is a mandatory parameter which specifies the string to be written.
  3. length : It is an optional parameter which specifies the maximum number of bytes to be written.

Return Value: It returns the number of bytes written on success, or False on failure.

Exceptions:

  1. Both binary data, like images and character data, can be written with this function since fwrite() is binary-safe.
  2. If writing operation is performed twice to the file pointer, then the data will be appended to the end of the file content.

Examples:

  Input : $myfile = fopen("gfg.txt", "w");          echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!");          fclose($myfile);  Output : 36    Input : $myfile = fopen("gfg.txt", "w");          echo fwrite($myfile, "PhP is Simple to Learn!");          echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!");          fclose($myfile);  Output : 23           59  

Below programs illustrate the fwrite() function:

Program 1:




<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");
  
// writing content to a file using fwrite()
echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!");
  
// closing the file
fclose($myfile);
?>
 
 

Output:

  36  

Program 2:




<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");
  
// writing content to a file using fwrite()
echo fwrite($myfile, "PhP is Simple to Learn!");
echo fwrite($myfile, "Geeksforgeeks is a portal for geeks!");
  
// closing the file
fclose($myfile);
?>
 
 

Output:

  23  59  

Reference : http://php.net/manual/en/function.fwrite.php



Next Article
PHP | is_dir( ) Function

S

Shubrodeep Banerjee
Improve
Article Tags :
  • PHP
  • Web Technologies
  • PHP-file-handling
  • PHP-function

Similar Reads

  • PHP basename( ) Function
    The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: string basename ( $path , $suffix ) Parameters: The basename() function in PHP accepts two parameters which are
    2 min read
  • PHP chgrp( ) Function
    The chgrp() function in PHP is an inbuilt function that is used to change the user group of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the group of a file arbitrarily. Syntax: bool chgrp ( $filename, $group ) Parameters: The chgrp(
    2 min read
  • PHP chmod( ) Function
    The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user. The chmod() function changes the permissions of the specified file and returns true on success and false on failure. Syntax: bool chmod ( string $filename, in
    2 min read
  • PHP chown( ) Function
    The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file. Syntax: bool chown ( $filename, $user ) Parameters: The chown() function in PHP
    2 min read
  • PHP copy( ) Function
    The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bo
    2 min read
  • PHP dirname( ) Function
    The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.'
    2 min read
  • PHP disk_free_space( ) Function
    The disk_free_space() function in PHP is an inbuilt function which is used to return the amount of free space in a specified directory. The disk_free_space() function denotes the free space in bytes. It returns the available space on a filesystem or on a disk partition. The disk_free_space() functio
    2 min read
  • PHP disk_total_space( ) Function
    The disk_total_space() function in PHP is an inbuilt function which is used to return the total space of a specified directory. The disk_total_space() function denotes the total space in bytes. It returns the total space on a filesystem or on a disk partition. The disk_total_space() function returns
    2 min read
  • PHP fclose() Function
    The fclose() function in PHP is an inbuilt function that is used to close a file that is pointed by an open file pointer. The fclose() function returns true on success and false on failure. It takes the file as an argument that has to be closed and closes that file. Syntax: bool fclose( $file )Param
    2 min read
  • PHP feof( ) Function
    The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the "end-of-file" has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand. The feof() func
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences