php 如何创建txt文件

兄弟们在线问下,php 如何创建txt文件
最新回答
少年先疯队队长

2026-10-29 19:19:58

有两种方法:

  1. file_put_contents("/tmp/a.txt","");

  2. system("cd /tmp; touch 1.txt"); 这种只时候在服务器执行,本地不可以

战皆罪

2026-01-03 01:12:46

看手册,文件操作部分,比如 file_put_contents 就能满足要求

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
懵蓝初梦

2026-06-17 00:52:46

<?php
$file = fopen("a.text",'w');
fwrite($file,"sdadsadasdas");
fclose($file);

?>