为什么 fopen() 会有这样的行为?

发布于 2024-09-17 16:49:30 字数 549 浏览 3 评论 0原文

我目前正在开发这个项目,该项目需要我创建一个动态决定目录名称的函数,然后在该目录中创建一个简单的 .txt 文件。

我的代码如下: ($destinatario 是一个字符串)

     $diretorio="../messages/".$destinatario;
if (is_dir($diretorio)) {
    ;
}else{
    mkdir($diretorio);
}
$path=$diretorio."/".$data.",".$assunto.",".$remetente.".txt";
$handle=fopen($path,'w+');

fwrite($handle, $corpo);

fclose($handle);

不用介意葡萄牙语,但底线是它应该使用我提供的命名指南创建一个 .txt 文件。有趣的是,当我这样做时,php 创建了这个奇怪的文件,其文件名是“01.09.2010 04” (根本没有扩展名)这相当于我想创建的实际文件名的前几个字符...

edit($data 实际上是调用 date("dmY H:i")) 的输出

I am currently working on this project which requires me to make a function which dinamically decides the directory name and then creates a simple .txt file in that directory.

my code is as follows:
($destinatario is a string)

     $diretorio="../messages/".$destinatario;
if (is_dir($diretorio)) {
    ;
}else{
    mkdir($diretorio);
}
$path=$diretorio."/".$data.",".$assunto.",".$remetente.".txt";
$handle=fopen($path,'w+');

fwrite($handle, $corpo);

fclose($handle);

nevermind the portuguese, but the bottom line is that it should create a .txt file using the naming guidelines i've provided. The funny thing is that when i do this, php creates this weird file whose filename is "01.09.2010 04"
(with no extension at all) which amounts to the first few characters of the actual filename i'd like to create...

edit($data is actually the output from a call to date("d.m.Y H:i"))

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

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

发布评论

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

评论(4

一抹微笑 2024-09-24 16:49:30

根据OP的评论:

[$data 是]实际上是调用 date("dmY H:i") 的输出

问题是 : 字符。 (不过,可能还有其他非法字符)组成最终文件名的其他部分中的字符。)


编辑
问题和解决方案的本质在于 @tchen 的回答< /a>.请记住,冒号在(部分?全部?)*nix 平台上是有效的文件名字符,但在 Windows 上无效。

Per comment by OP:

[$data is] actually the output of a call to date("d.m.Y H:i")

The problem is the : character. (Still, there may be other illegal characters in the other parts composing the final file name.)


EDIT
The essence of the problem and solution is in the comments to @tchen's answer. Keep in mind that colon is a valid file name character on (some? all?) *nix platforms but is invalid on Windows.

因为看清所以看轻 2024-09-24 16:49:30

确保 $data 末尾没有坏字符。调用它的trim()。

如果它是从文件中获取的数据,则其末尾可能有“\r”或“\n”。

Make sure there's no bad characters at the end of $data. Call trim() on it.

If it's data taken from a file, it may have a '\r' or '\n' at the end of it.

ゞ记忆︶ㄣ 2024-09-24 16:49:30

不相关,但请确保您的 if 语句没有未使用的条件:

if (!is_dir($diretorio)) {
    mkdir($diretorio);
}

这也会用单个终止符 ; 删除该空行,我确信这是不对的。

Not related, but make sure your if statements don't have unused conditions:

if (!is_dir($diretorio)) {
    mkdir($diretorio);
}

This will also get rid of that blank line with a single terminator ;, I'm sure that isn't right.

◇流星雨 2024-09-24 16:49:30

一些想法:

  • 您是否尝试过在文件名中不使用逗号?
  • 你检查过fopen和fwrite的返回值吗?

只是为了尝试隔离问题,

您也可以简化为:

if (!is_dir($diretorio)) {
  mkdir($diretorio);
}

Some ideas:

  • have you tried not using commas in the filename?
  • Have you checked the return value if fopen and fwrite?

Just to try to isolate the problem

also you can simplify to:

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