Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»PHP»PHP Write to File – fwrite() Function

    PHP Write to File – fwrite() Function

    By RahulSeptember 6, 20161 Min Read

    PHP fwrite() function used to write data to file. This function take three arguments

    1. File specifies filename to write
    2. String specifies the string to write to file
    3. Lenght specifies maximum number of bytes to write to file (optional)

    Syntax

    PHP fwrite() function uses following syntax

     fwrite(file, string, length)
    

    Example

    Below is the sample example of writing data to file using php script. In below example first line will open myfile.txt in edit mode in current directory. Second line will write string to file, you can also specify the lenght here and result the number of bytes written. In case of failure, FALSE will be returned. Third line will close open file.

    <?php
      $fn = fopen("myfile.txt","w");
      $result = fwrite($fn,"Welcome to PHP!");
      fclose($fn);
    ?>
    

    Output:

    This function returns total number of bytes written to a file or returns FALSE in case of failure.

    file fwrite PHP
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Python Program to Copy a File

    How to Disable Functions in PHP

    How to Schedule a Cron Job for PHP: A Step-by-Step Guide

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PIP on macOS: A Comprehensive Guide
    • Find Objects Between Two Dates in MongoDB: A Practical Guide
    • How to Check Packages Update History in Ubuntu
    • How to Grep for Contents after a Matching Pattern
    • How to Change Port in Next.Js
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.