【问题标题】:PHP Create a .txt file with date as a filenamePHP 创建一个以日期为文件名的 .txt 文件
【发布时间】:2013-08-25 02:20:37
【问题描述】:

是否可以在使用 fopen 创建/打开文件时使用日期作为文件名? 我有一个将用户数据写入 txt 文件的表单,但我希望每次用户提交信息时,都会生成带有文件名日期的新 txt 文件。

这是我的尝试

$myfile='/home/myaccount/public_html/test/tiog/'.date('m-d-Y_hia').'.txt';
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $upisufajl);
fclose($fh);

我得到的错误是

Warning: fopen() [function.fopen]: Filename cannot be empty in /home/...

我如何将其编码为 UTF8,使其支持特殊字符 (čćšđž)?

谢谢

【问题讨论】:

  • 您可以尝试使用元方法<meta http-equiv="content-type" content="text/html; charset=UTF-8"> --- <meta charset="UTF-8">,并将文件本身编码为UTF-8,应该可以解决问题。
  • 我的php脚本是utf8,但是它创建的txt文件不是
  • @JeremyStewart 然后您还需要将这些 (.txt) 保存为 UTF-8。我使用记事本++,它对我很有效。
  • @Fred-ii- 该评论被误导了......
  • @Christian 那你有什么建议?

标签: php apache fopen


【解决方案1】:

Jeremy,您收到该错误是因为您的变量命名错误:

$myfile= .... pen($myFile ....
   ^                 ^
   '-----------------'--------- see?

您遇到的第二个问题(关于文件编码)是因为文本文件通常是纯 ascii。如果你想保存为 UTF-8,那完全没问题……只要确保你保存的字符串是 UTF-8 格式。

例子:

file_put_contents($myfile, utf8_encode($upisufajl));

【讨论】:

  • 如果对您有所帮助,请将其标记为正确答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2019-06-21
相关资源
最近更新 更多