Home > Backend Development > PHP Tutorial > Usage of php fopen function (open and create files)_PHP tutorial

Usage of php fopen function (open and create files)_PHP tutorial

WBOY
Release: 2016-07-20 11:02:07
Original
1534 people have browsed it

php tutorial fopen function usage (open and create files)
Many times we need to create files, especially when generating static pages. We often use the fopen function in php to create new files. Its original function is to append the file if it exists. If it does not exist, it can be created. Next There are many parameters, you can define them according to your needs.
$xxx = fopen("text.txt", 'w', true)

*/
$fopen = 'fopen.txt';
$str ='Write a file www.bkjia.com';
if( file_exists( $fopen ) )
{
$h = fopen($fopen,'a+');
if( fwrite($h,$str) )
{
echo 'Content added successfully';
}
fclose();
}
else
{
$h = fopen( $fopen,'w+');
if( fwrite($h,$str))
{
echo 'File written successfully';
}
fclose();
}

$file = fopen("test.txt","r");
$file = fopen("/home/mb.php100.com/test.txt","r");
$file = fopen("/home/test/test.gif","wb");
$file = fopen("http://www.bkjia.com/","r");
$file = fopen("ftp://user:password@example.com/test.txt","w");


/*
Detailed description of fopen function

Definition and usage
The fopen() function opens a file or url.

If the opening fails, this function returns false.

Grammar
fopen(filename,mode,include_path,context) parameter description
filename required. Specifies the file or url to open.
mode is required. Specifies the type of access required to this file/stream. Possible values ​​are shown in the table below.
include_path is optional. If you also need to retrieve files in include_path, you can set this parameter to 1 or true.
context is optional. Regulations


"r" opens in read-only mode, pointing the file pointer to the file header.
"r+" opens in read-write mode and points the file pointer to the file header.
"w" turns on writing mode, points the file pointer to the file header and truncates the file size to zero. create.
"w+" opens in read-write mode, points the file pointer to the file header and truncates the file size to zero. create.
"a" opens in writing mode and points the file pointer to the end of the file. File does not exist created.
"a+" opens in read-write mode and points the file pointer to the end of the file. File does not exist created.

*/
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445397.htmlTechArticlephp tutorial fopen function usage (open and create files) Many times we need to create files, especially to generate static pages Time, we often use the fopen function in php...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template