file_put_contents failed to open stream: Invalid

前两天,使用file_put_contents方法, 传入两个实参,将内容写入文件,一直报错

file_put_contents(E:\config.php,content): failed to open stream: Invalid argument in 。。。

不多说,一番百度,终于寻得前辈留下的葵花宝典:

http://www.howtobuildsoftware.com/index.php/how-do/Fp6/php-php-file-put-contents-failed-to-open-stream-invalid-argument

内容如下:

Please help! I have tried figuring this out for over an hour What i am trying to do is to get form values via POST, and then put these into a timestamped txt file The error i get is:

Warning: file_put_contents(D:\wamp\www\weboldal\hu\php\en\03-14-15-11:08:48.txt): failed to open stream: Invalid argument in D:\wamp\www\weboldal\hu\php\en\formprocessing2.php on line 18

if ($_POST["type"] == "Type1") {
    print "Type1";
    $filename = date("m-d-y-h:i:s");
    $fullfilename = "D:\\wamp\\www\\weboldal\\hu\\php\\en\\".$filename.".txt";
    print $fullfilename;
    while (file_exists($fullfilename)) {
        print "Please wait a few seconds, server is busy";
        sleep(1);
    }
    $type = $_POST["type"];
    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $contents = $type."r\n".$name."r\n".$email."r\n".$password;
    file_put_contents($fullfilename,$contents,LOCK_EX);

EDIT: The problem was that windows would not accept colons in the filename, thanks to user @Hobo Sapiens for pointing that out

Best How To 

Windows will reject the filename with the timestamp as it is because of the colons(冒号). Remove those and you should be fine.

 

 

上面代码:file_put_contents($fullfilename,$contents,LOCK_EX);只是多传入LOCK_EX这个参数,在文件写入的时候进行锁定,问题解决!

但是,疑问也来了,

colons(冒号)LOCK_EX锁定有啥联系?

求大神稍稍指点迷津

 

 

 

 

转载于:https://my.oschina.net/u/3567851/blog/1841093

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值