前兩天,使用file_put_contents方法, 傳入兩個實參,將內容寫入文件,一直報錯php
file_put_contents(E:\config.php,content): failed to open stream: Invalid argument in 。。。python
很少說,一番百度,終於尋得前輩留下的葵花寶典:web
http://www.howtobuildsoftware.com/index.php/how-do/Fp6/php-php-file-put-contents-failed-to-open-stream-invalid-argumentwindows
內容以下:api
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:ui
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 18this
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 outspa
Windows will reject the filename with the timestamp as it is because of the colons(冒號). Remove those and you should be fine..net
上面代碼:file_put_contents($fullfilename,$contents,LOCK_EX);只是多傳入LOCK_EX這個參數,在文件寫入的時候進行鎖定,問題解決!code
可是,疑問也來了,
colons(冒號)和LOCK_EX(鎖定)有啥聯繫?
求大神稍稍指點迷津