Home >Backend Development >PHP Tutorial >Summary of various methods of creating files in php_PHP tutorial

Summary of various methods of creating files in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:571250browse

There are many ways to create files in php. The two most commonly used methods are fopen and file_put_contents. Let me give you a detailed introduction. Students who need to know more can refer to it.

Create php file

The code is as follows Copy code
 代码如下 复制代码

$str="";
file_put_contents('test.php',$str);//使用脚本创建一个php文件
?>

$str="";
file_put_contents('test.php',$str);//Use a script to create a php file
?>

Example 2
 代码如下 复制代码

if ($argc != 2) {
die("Usage: php mkphp.php filename");
}
array_shift($argv);
$cat= $argv[0];
file_put_contents($cat.".php", "

?>");

The code is as follows Copy code

if ($argc != 2) {
die("Usage: php mkphp.php filename");
}
array_shift($argv);
$cat= $argv[0];
file_put_contents($cat.".php", "

代码如下 复制代码

$fp=fopen("1.txt","w+");//fopen()的其它开关请参看相关函数
$str="我加我加我加加加";
fputs($fp,$str);
fclose($fp);
?>

?>");

Use fopen to create files
 代码如下 复制代码

$filename="test.txt";
$fp=fopen("$filename", "w+"); //打开文件指针,创建文件
if ( !is_writable($filename) ){
die("文件:" .$filename. "不可写,请检查!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //关闭指针

The code is as follows Copy code
<🎜> <🎜>$fp=fopen("1.txt","w+");//For other switches of fopen(), please refer to related functions
$str="I add, I add, I add, add, add";
fputs($fp,$str);
fclose($fp);
?>

The above has not been considered in any way. If we want to be more comprehensive, we must first determine the permissions of the directory where the file you want to create is located; the recommended device is 777. Then, it is recommended to use an absolute path for the name of the new file.
The code is as follows Copy code
$filename="test.txt";
$fp=fopen("$filename", "w+"); //Open the file pointer and create the file
if ( !is_writable($filename) ){
​​​die("File:" .$filename. "Not writable, please check!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //Close pointer

'r' opens the file as read-only, and the file pointer points to the beginning
'r+' opens the file for reading and writing, and the file pointer points to the beginning
'w' opens the file for writing, points to the beginning of the file, and sets the length of the original text to 0. If the file does not exist ‘‘Create a new file –
'w+' opens the file in read-write mode, points to the beginning of the file, and sets the length of the original text to 0. If the file does not exist ‘‘Create a new file –
'a' opens the file for writing, and the file pointer 'points to the end of the file. If the file does not exist ‘‘Create a new file –
'a+' opens the file in read-write mode, and the file pointer ' points to the end of the file. If the file does not exist ‘‘Create a new file –
'b' If the text and binary files of the operating system are different, you can use "‘", but UNIX systems do not need to use "parameters".

The code is as follows
 代码如下 复制代码

///创建文件
function creat_file($PATH){
   $sFile = "test.html";
   if (file_exists($PATH.$sFile)) {
    creat_file();
   } else {
    $fp= fopen($PATH.$sFile,"w");
    fclose($fp);
   }
   return $sFile;
}

Copy code
///Create file function creat_file($PATH){

$sFile = "test.html";

if (file_exists($PATH.$sFile)) { } else { $fp= fopen($PATH.$sFile,"w"); fclose($fp); } Return $sFile; }
http://www.bkjia.com/PHPjc/631558.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631558.htmlTechArticlephp There are many ways to create files. The two most commonly used methods are fopen and file_put_contents to create files. , let me give you a detailed introduction below. If you need to know more...
Statement:
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