I need to create a series of files and I'm using the following method
$file = "myFile.txt";
$fhandle = 开发者_开发问答fopen($file, 'w') or die("can't open file");
fclose($fhandle);
Are there better ways of doing this?
You can use touch() or file_put_contents() with an empty string. These would only require one function call.
touch("test.txt");
I'm not sure if this will work on non-unix platforms?
You can use
<?php
file_put_contents ($filename , "");
?>
on a Linux / Unix Server you could use system to do it via a shell command, but I would rather stay with the PHP functions (for Security and portability reasons) - anyway:
<?php
system('touch '.escapeshellarg($filename));
?>
You could use File_Put_Contents but there isn't any advantage of using that over your method (other than it's shorter).
Just make sure your user (typically www-data on Apache servers) has permission to edit the directory that file will exist on.
精彩评论