今天用file_put_content($fileName,$data)产生错误:内容如下:
Warning: file_put_contents(images/7d5636992a7395f91744747ffa0ecf0b.gif?b=bgif): failed to open stream: No error in F:xampphtdocsphpwebCrawlcrawl.php
images下如果没有这个文件则会创建,存在则覆盖,为什么failed.这是因为文件名7d5636992a7395f91744747ffa0ecf0b.gif?b=bgif
包含了windows文件名无效字符。
无效字符有:
/ : * ? " < > |
写一个函数,替换无效字符;
function replaceBadChar($fileName) { // 去掉文件名中的无效字符,如 / : * ? " < > | $fileName=str_replace('\','_',$fileName); $fileName=str_replace('/','_',$fileName); $fileName=str_replace(':','_',$fileName); $fileName=str_replace("*",'_',$fileName); $fileName=str_replace("?",'_',$fileName); $fileName=str_replace('"','_',$fileName); $fileName=str_replace('<','_',$fileName); $fileName=str_replace('>','_',$fileName); $fileName=str_replace('|','_',$fileName); return $fileName; }
即可。