Heim  >  Artikel  >  Backend-Entwicklung  >  PHP怎么创建文件并且写入文字

PHP怎么创建文件并且写入文字

WBOY
WBOYOriginal
2016-06-23 13:50:341141Durchsuche

我要封装个方法 创建文件并且写入文字 参数2个 width和height

实行后的效果     
比方说如100 300

生成文件名叫s 100300.less

里面内容是
@import "shiying";
@screen_width: 100px;
@screen_height: 300px;


这个函数怎么写。。 红色的要对应哦


回复讨论(解决方案)

function mkless($width, $height) {  $r[] = '@import "shiying";';  $r[] = sprintf('@screen_width:%spx;', $width);  $r[] = sprintf('@screen_height:$spx;', $height);  file_put_contents("s$width$height.less", join(PHP_EOL, $r));}

function mkless($width, $height) {  $r[] = '@import "shiying";';  $r[] = sprintf('@screen_width:%spx;', $width);  $r[] = sprintf('@screen_height:$spx;', $height);  file_put_contents("s$width$height.less", join(PHP_EOL, $r);}


Parse error: syntax error, unexpected ';' in D:\wamp\www\ceshi\xieru.php on line 14
报错了 而且这个fileputcontent不能自动生成文件吧。。

 file_put_contents("s$width$height.less", join(PHP_EOL, $r);
少个括号

file_put_contents() 函数把一个字符串写入文件中。

与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
语法

file_put_contents(file,data,mode,context)

参数  描述
file  必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。
....
...

就是个函数调用 的问题

file_put_contents

顺便符上代码

$filename='s100300.less';$date='@import "shiying";@screen_width:100px;@screen_height:300px;';file_put_contents($filename,$date);

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn