使用 PHP 处理文件

发布于 2024-09-09 00:11:11 字数 37 浏览 1 评论 0原文

任何人都可以告诉使用 PHP 修改/删除文本文件内容的方法吗

Can anyone tell the method to modify/delete the contents of a text file using PHP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很快妥协 2024-09-16 00:11:11

使用 file_put_contents

file_put_contents($filename, 'file_content');

如果您想追加到文件而不是替换其内容,请使用:(

file_put_contents($filename, 'append_this', FILE_APPEND);

file_out_contents 是使用整个 fopen 的更简单替代方案复杂的。)

Using file_put_contents:

file_put_contents($filename, 'file_content');

If you want to append to the file instead of replacing it's contents use:

file_put_contents($filename, 'append_this', FILE_APPEND);

(file_out_contents is the simpler alternative to using the whole fopen complex.)

星星的轨迹 2024-09-16 00:11:11

通过使用 fopen

if (is_writable($filename)) {
  if (!$handle = fopen($filename, 'w')) {
     echo "Cannot open file ($filename)";
     exit;
  }
  if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
  }

  echo "Success, wrote to file ($filename)";

  fclose($handle);
}

将“”写入文件以删除其内容。

要逐行删除内容,请使用:

$arr = file($fileName);

unset($arr[3]); // 3 is an arbitrary line

然后写入文件内容。或者您指的是内存映射文件?

By using fopen:

if (is_writable($filename)) {
  if (!$handle = fopen($filename, 'w')) {
     echo "Cannot open file ($filename)";
     exit;
  }
  if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
  }

  echo "Success, wrote to file ($filename)";

  fclose($handle);
}

Write "" to a file to delete its contents.

To delete contents line by line use:

$arr = file($fileName);

unset($arr[3]); // 3 is an arbitrary line

then write the file contents. Or are you referring to memory mapped files?

牛↙奶布丁 2024-09-16 00:11:11

读取文件的方法有很多种。 可以非常快速地处理大文件

$fd = fopen ("log.txt", "r"); // you can use w/a switches to write append text
while (!feof ($fd)) 
{ 
   $buffer = fgets($fd, 4096); 
   $lines[] = $buffer; 
} 
fclose ($fd); 

使用您还可以使用 file_get_contents() 来读取文件, 。这是实现同样目标的简短方法。

要修改或附加文件,您可以使用

$filePointer = fopen("log.txt", "a");
fputs($filePointer, "Text HERE TO WRITE");
fclose($filePointer);

“您还可以将文件加载到数组中,然后执行搜索操作来删除数组的特定元素”。

$lines = file('带有完整路径的文件。'); // 路径中的单斜杠应该是双斜杠,例如 C://PATH //TO //FILE.TXT
上面的代码将加载 $lines 数组中的文件。

There are number of ways to read files. Large files can be handled very fast using

$fd = fopen ("log.txt", "r"); // you can use w/a switches to write append text
while (!feof ($fd)) 
{ 
   $buffer = fgets($fd, 4096); 
   $lines[] = $buffer; 
} 
fclose ($fd); 

you can also use file_get_contents() to read files. its short way of achieving same thing.

to modify or append file you can use

$filePointer = fopen("log.txt", "a");
fputs($filePointer, "Text HERE TO WRITE");
fclose($filePointer);

YOU CAN ALSO LOAD THE FILE INTO ARRAY AND THEN PERFORM SEARCH OPERATION TO DELETE THE SPECIFIC ELEMENTS OF THE ARRAY.

$lines = file('FILE WITH COMPLETE PATH.'); // SINGLE SLASH SHOULD BE DOUBLE SLASH IN THE PATH SOMTHING LIKE C://PATH//TO//FILE.TXT
Above code will load the file in $lines array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文