in php how do I determine whether or not I can create a file in the same path as the script trying to create a file
A:
Use is_writable
PHP function, documentation and example source code of this you can find at http://pl2.php.net/manual/en/function.is-writable.php
Svisstack
2010-08-29 21:35:09
+2
A:
Have you tries the is_writable() function ?
Documentation http://www.php.net/manual/en/function.is-writable.php
Example:
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
vlad b.
2010-08-29 21:36:10
A:
The is_writable function is good stuff. However, the OP asked about creating a file in the same directory as the script. Blatantly stealing from vlad b, do this:
$filename = __DIR__ . '/test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
See the php manual for predefined constants for the details on __DIR__
. Without it, you're going to create a file in the current working directory, which is probably more or less undefined for your purposes.
easel
2010-08-29 21:46:29