2012-05-26 224 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"));