2012-05-26 65 views
1

是否有任何脚本可以在指定的目录中创建文件? 像创建一个php文件

<?php

createfile("hi.txt","/home/public_html/files");

?>

+0

[''file_put_contents(“/ path/filename.txt”,“把这东西写到文件...”);'](http://us3.php.net/manual/en/function。 file-put-contents.php) –

+1

[我如何在服务器上创建文件?](http://stackoverflow.com/questions/5133999/how-do-i-create-file-on-server) –

+0

另外http://stackoverflow.com/questions/9752904/write-to-create-file-in-php –

回答

4

您可以使用file_put_contents

<?php 

file_put_contents("/home/public_html/files/hi.txt", "content"); 

?> 

或者touch如果你不需要的内容,只是希望确保该文件被创建:

<?php 

touch("/home/public_html/files/hi.txt"); 

?> 
+0

我该怎么做到这一点?你能给我一个示例代码 –

+0

@MatthewJones去链接。这本手册有很多例子和更好的描述每个功能。 – meze

+1

@MatthewJones - 我同意meze,链接足以让你开始。如果你开始使用PHP,我会推荐一些教程/书籍和一些扎实的研究 - 这是通过初学者阶段的最好方法':)' – halfer

1

也可以尝试fopen("/path/to/file", 'w')

“W” 将根据the docs“如果文件不存在,试图创建它。 “

0

也许this是对某些人询问是否使用file_put_contents()touch()

0

简单:

fwrite($file = fopen($filename, "w"), "New content"); 
fclose($file); 

如果文件不包含任何内容:

fclose($file = fopen($filename, "w")); 

而且,检查文件是否存在与file_exists()以防止清除现有文件的内容:

if (!file_exists($filename)) 
    fclose($file = fopen($filename, "w"));