Apr 21st 2013, 16:40:35
First off, you should make sure you are sanitizing all user input. Trusting the contents of $_POST can lead to all sorts of security vulnerabilities.
It sounds like what you are looking for is the PHP file_exists function.
The manual entry:
http://php.net/.../function.file-exists.php
And an example usage:
$exists = file_exists('somefile.csv');
if(!$exists) {
// create the file if it was not found
}
// your file processing continues here.
Now, that said, if you just use the fopen function on a filename that is not found, PHP will attempt to create the file. specifically:
$my_csv = fopen('somefile.csv','a');
will open somefile.csv with the pointer at the end of the file (so that if you write to the file you will not overwrite its current contents) if somefile.csv exists and, if somefile.csv is not found by PHP, will attempt to create somefile.csv automatically.
It sounds like what you are looking for is the PHP file_exists function.
The manual entry:
http://php.net/.../function.file-exists.php
And an example usage:
$exists = file_exists('somefile.csv');
if(!$exists) {
// create the file if it was not found
}
// your file processing continues here.
Now, that said, if you just use the fopen function on a filename that is not found, PHP will attempt to create the file. specifically:
$my_csv = fopen('somefile.csv','a');
will open somefile.csv with the pointer at the end of the file (so that if you write to the file you will not overwrite its current contents) if somefile.csv exists and, if somefile.csv is not found by PHP, will attempt to create somefile.csv automatically.