pTutorial

New way to learn PHP

PHP 5 File write or create

Write file

In this section we will learn how to write file data on server.

fwrite function is used to write the content of string into the file file specified by filepointer. It's takes two parameter first is file pointer and second is lenght or byte of content.

Syntax of fopen

int fwrite (int filepointer,string [, int length])

If the optional input parameter length is provided, writing will stop either after length characters have been written or after the end of string has been reached.

Example

<?php

$fh = fopen("ptutorial.txt","w") ;

$u="Mr. John ";

fwrite($fh, $u);

?>

In this case if pt.txt is not exist then fopen function create then after fwrite function write the string on it, if file is exist then owerwriter the content of file.

Text inside the file pt.txt

Mr. John

Example

<?php

$fh = fopen("ptutorial1.txt","w") ;

$u="Mr brown";

fwrite($fh, $u);

$u="Mr victor";

fwrite($fh, $u);

fclose($fh);

$fh1 = fopen("pt.txt","r") ;

$s=fread($fh1,50);

print $s;

fclose($fh1);

?>

Output

Mr brownMr victor

As i told you w is used for write mode only so that i opened this file two times first time for write something and second time for reading the file data.


QUOTES OF THE DAY

Knowledge without integrity is dangerous and dreadful.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER