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.
*/
?>