¿Cómo puedo comprobar si un directorio es escribible en PHP?


¿Alguien sabe cómo puedo comprobar si un directorio es escribible en PHP?

La función is_writable no funciona para las carpetas. (editar: Funciona. Ver la respuesta aceptada.)

Author: Gras Double, 2008-09-21

9 answers

Sí, funciona para carpetas....

Devuelve TRUE si el nombre del archivo existe y se puede escribir. El argumento filename puede ser un nombre de directorio que le permite comprobar si un directorio es escribible.

 86
Author: DGM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-20 20:09:44

Este es el código :)

<?php 

$newFileName = '/var/www/your/file.txt';

if ( ! is_writable(dirname($newFileName))) {

    echo dirname($newFileName) . ' must writable!!!';
} else {

    // blah blah blah
}
 17
Author: Irfan EVRENS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-05 07:15:51

Para ser más específico para propietario/grupo / mundo

$dir_writable = substr(sprintf('%o', fileperms($folder)), -4) == "0774" ? "true" : "false";

Paz...

 6
Author: Griffith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-25 07:38:58

Puede estar enviando una ruta de archivo completa a la función is_writable(). is_writable() devolverá false si el archivo no existe ya en el directorio. Es necesario comprobar el propio directorio con el nombre de archivo eliminado, si este es el caso. Si lo hace, is_writable le dirá correctamente si el directorio es escribible o no. Si $file contiene la ruta del archivo, haga lo siguiente:

$file_directory = dirname($file);

Luego use is_writable($file_directory) para determinar si la carpeta es escribible.

Espero que esto ayude a alguien.

 5
Author: A J,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-18 11:51:13

De acuerdo con la documentación para is_writable, debería funcionar, pero dijiste "carpeta", por lo que esto podría ser un problema de Windows. Los comentarios sugieren una solución alternativa.

(Una lectura apresurada antes me hizo pensar que las barras finales eran importantes, pero que resultó ser específico para este trabajo).

 4
Author: Quentin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-21 09:52:46

Stat ()

Muy parecido a una estadística del sistema, pero en PHP. Lo que desea comprobar es el valor del modo, al igual que lo haría fuera de cualquier otra llamada a stat en otros idiomas (I. E. C/C++).

Http://us2.php.net/stat

 2
Author: Jason Mock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-20 20:12:29

De acuerdo con el manual de PHP is_writable debería funcionar bien en directorios.

 1
Author: Lasar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-20 20:09:45

Así es como lo hago:

Crea un archivo con file_put_contents() y comprueba el valor devuelto, si es positivo (número de bytes escritos) entonces puedes seguir adelante y hacer lo que tengas que hacer, si es FALSO entonces no es escribible

$is_writable = file_put_contents('directory/dummy.txt', "hello");

if ($is_writable > 0) echo "yes directory it is writable";

else echo  "NO directory it is not writable";

Luego puede eliminar el archivo ficticio usando unlink ()

unlink('directory/dummy.txt');
 1
Author: Nassim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-16 12:11:57

He escrito un pequeño script (lo llamo isWritable.php ) que detecta todos los directorios en el mismo directorio en el que se encuentra el script y escribe en la página si cada directorio es escribible o no. Espero que esto ayude.

<?php
// isWritable.php detects all directories in the same directory the script is in
// and writes to the page whether each directory is writable or not.

$dirs = array_filter(glob('*'), 'is_dir');

foreach ($dirs as $dir) {
    if (is_writable($dir)) {
        echo $dir.' is writable.<br>';
    } else {
        echo $dir.' is not writable. Permissions may have to be adjusted.<br>';
    } 
}
?>
 1
Author: Studocwho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-10 02:39:15