tags:

views:

60

answers:

5

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?

A: 

You could use File_Put_Contents but there isn't any advantage of using that over your method (other than it's shorter).

Jan Hančič
+2  A: 

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));
?>
crono
+7  A: 

You can use touch() or file_put_contents() with an empty string. These would only require one function call.

Tom Haigh
+4  A: 
touch("test.txt");

I'm not sure if this will work on non-unix platforms?

Steve Claridge
It didn't modify the last-modified time on Windows until PHP 5.3.0.
Dominic Rodger
A: 

Just make sure your user (typically www-data on Apache servers) has permission to edit the directory that file will exist on.

Kyle J. Dye