How to check if there are write permissions in the current path

in php, how do I determine if I can create a file in the same path as the script, trying to create the file

+3
source share
4 answers

Are you trying to use 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';
}
+5
source

Unfortunately, all answers are still erroneous or incomplete.

is_writable

Returns TRUE if the file name exists and is writable

It means that:

is_writable(__DIR__.'/file.txt');

Will return false, even if the script has write permissions to the directory, because file.txt does not yet exist.

, , :

is_writable(__DIR__);

, , , :

function isFileWritable($path)
{
    $writable_file = (file_exists($path) && is_writable($path));
    $writable_directory = (!file_exists($path) && is_writable(dirname($path)));

    if ($writable_file || $writable_directory) {
        return true;
    }
    return false;
}
+5

is_writable - . , script. vlad b, :

$filename = __DIR__ . '/test.txt';
if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

__DIR__ . php . , , , undefined .

+3

Source: https://habr.com/ru/post/1762185/


All Articles