What are the file modes in php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    File handling is needed for any application. For some tasks to be done file needs to be processed. File handling in PHP is similar as file handling is done by using any programming language like C. PHP has many functions to work with normal files. Those functions are:

    1) fopen() – PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened and second parameter tells about mode in which file needs to be opened, e.g.,

    $file = fopen(“demo.txt”,'w');

    ?>

    Files can be opened in any of the following modes :

    • “w” – Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is erased.
    • “r” – File is opened for read only.
    • “a” – File is opened for write only. File pointer points to end of file. Existing data in file is preserved.
    • “w+” – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file is erased.
    • “r+” – File is opened for read/write.
    • “a+” – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
    • “x” – New file is created for write only.

    2) fread() –– After file is opened using fopen() the contents of data are read using fread(). It takes two arguments. One is file pointer and another is file size in bytes, e.g.,

    $filename = "demo.txt";

    $file = fopen( $filename, 'r' );

    $size = filesize( $filename );

    $filedata = fread( $file, $size );

    ?>

    3) fwrite() – New file can be created or text can be appended to an existing file using fwrite() function. Arguments for fwrite() function are file pointer and text that is to written to file. It can contain optional third argument where length of text to written is specified, e.g.,

    $file = fopen("demo.txt", 'w');

    $text = "Hello world\n";

    fwrite($file, $text);

    ?>

    4) fclose() – file is closed using fclose() function. Its argument is file which needs to be closed, e.g.,

    $file = fopen("demo.txt", 'r');

    fclose($file);

    ?>

    Reference –
    Wikipedia

    This article is contributed by Swasthik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    next → ← prev

    PHP fopen() function is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.

    Syntax

    PHP Open File Mode

    ModeDescription
    r Opens file in read-only mode. It places the file pointer at the beginning of the file.
    r+ Opens file in read-write mode. It places the file pointer at the beginning of the file.
    w Opens file in write-only mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
    w+ Opens file in read-write mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
    a Opens file in write-only mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
    a+ Opens file in read-write mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
    x Creates and opens file in write-only mode. It places the file pointer at the beginning of the file. If file is found, fopen() function returns FALSE.
    x+ It is same as x but it creates and opens file in read-write mode.
    c Opens file in write-only mode. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file
    c+ It is same as c but it opens file in read-write mode.

    PHP Open File Example

    Next TopicPHP Read File

    ← prev next →


    What are the file modes in php?
    For Videos Join Our Youtube Channel: Join Now


    Feedback

    • Send your Feedback to [email protected]

    Help Others, Please Share

    What are the file modes in php?
    What are the file modes in php?
    What are the file modes in php?





    How many file handling modes are there in PHP?

    In PHP file handling, there are four sets of possible modes. These are, {r and r+} – To read the existing files.

    What are files in PHP?

    So what exactly is a PHP file? Generally speaking, a PHP file is a plain-text file which contains code written in the PHP programming language. Since PHP is a server-side (back-end) scripting language, the code written in the PHP file is executed on the server.

    What is file handling in PHP explain it with different modes with example of each?

    File Handling in PHP: Opening Files.

    What are file functions?

    File functions.