为什么 fopen() 会有这样的行为?
我目前正在开发这个项目,该项目需要我创建一个动态决定目录名称的函数,然后在该目录中创建一个简单的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据OP的评论:
问题是
:
字符。 (不过,可能还有其他非法字符)组成最终文件名的其他部分中的字符。)编辑
问题和解决方案的本质在于 @tchen 的回答< /a>.请记住,冒号在(部分?全部?)*nix 平台上是有效的文件名字符,但在 Windows 上无效。
Per comment by OP:
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.
确保 $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.
不相关,但请确保您的 if 语句没有未使用的条件:
这也会用单个终止符
;
删除该空行,我确信这是不对的。Not related, but make sure your if statements don't have unused conditions:
This will also get rid of that blank line with a single terminator
;
, I'm sure that isn't right.一些想法:
只是为了尝试隔离问题,
您也可以简化为:
Some ideas:
Just to try to isolate the problem
also you can simplify to: