sourcecode

PHP로 디렉토리를 쓸 수 있는지 확인하려면 어떻게 해야 합니까?

copyscript 2023. 8. 22. 22:26
반응형

PHP로 디렉토리를 쓸 수 있는지 확인하려면 어떻게 해야 합니까?

PHP로 디렉토리를 쓸 수 있는지 확인하는 방법을 아는 사람이 있습니까?

폴더에 대해서는 기능이 작동하지 않습니다.

편집: 효과가 있습니다.승인된 답변을 참조하십시오.

예, 폴더에 사용할 수 있습니다.

파일 이름이 존재하고 쓰기 가능한 경우 TRUE를 반환합니다.filename 인수는 디렉토리가 쓰기 가능한지 확인할 수 있는 디렉토리 이름일 수 있습니다.

이것이 코드입니다 :)

<?php 

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

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

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

    // blah blah blah
}

소유자/그룹/세계에 대해 좀 더 구체적으로 설명하기 위해

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

평화...

전체 파일 경로를 에 보낼 수 있습니다.is_writable()기능.is_writable()디렉터리에 파일이 아직 없으면 false를 반환합니다.이 경우 파일 이름이 제거된 디렉토리 자체를 확인해야 합니다.그렇게 하면,is_writable디렉토리가 쓰기 가능한지 여부를 올바르게 알려줍니다.$file다음 작업을 수행할 파일 경로를 포함합니다.

$file_directory = dirname($file);

사용할 경우is_writable($file_directory)폴더가 쓰기 가능한지 여부를 확인합니다.

이것이 누군가에게 도움이 되길 바랍니다.

is_writeable에 대한 설명서에 따르면 이 기능은 작동해야 하지만 "folder"라고 했으므로 Windows 문제일 수 있습니다.이 의견은 해결 방법을 제시합니다.

(앞서 급하게 읽은 내용은 슬래시의 후행이 중요하다고 생각했지만, 이 작업에 특정적인 것으로 나타났습니다.)

스크립트가 있는 동일한 디렉토리에 있는 모든 디렉토리를 검색하여 각 디렉토리가 쓰기 가능한지 여부를 페이지에 기록하는 작은 스크립트(Writable.php라고 부릅니다)를 작성했습니다.이게 도움이 되길 바랍니다.

<?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>';
    } 
}
?>

통계청

시스템 상태와 매우 비슷하지만 PHP에 있습니다.다른 언어(I.E.C/C++)로 stat을 호출할 때와 마찬가지로 모드 값을 확인합니다.

http://us2.php.net/stat

PHP에 따르면 is_writable 매뉴얼은 디렉토리에서 잘 작동합니다.

저 같은 경우에는.is_writabletrue를 반환했지만 파일을 쓰려고 할 때 오류가 발생했습니다.
이 코드는 다음과 같은지 확인하는 데 도움이 됩니다.$dir존재하며 쓰기 가능:

<?php
$dir = '/path/to/the/dir';

// try to create this directory if it doesn't exist
$booExists     = is_dir($dir) || (mkdir($dir, 0774, true) && is_dir($dir));
$booIsWritable = false;
if ($booExists && is_writable($dir)) {
    $tempFile = tempnam($dir, 'tmp');
    if ($tempFile !== false) {
        $res = file_put_contents($tempFile, 'test');

        $booIsWritable = $res !== false;
        @unlink($tempFile);
    }
}

이게 내가 하는 방식입니다.

파일을 만듭니다.file_put_contents()반환 값이 양수이면(바이트 단위로 기록된 수), FALSE이면 쓰기 가능하지 않은 값을 확인할 수 있습니다.

$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";

그런 다음 링크 해제를 사용하여 더미 파일을 삭제할 수 있습니다.

unlink('directory/dummy.txt');

언급URL : https://stackoverflow.com/questions/109188/how-do-i-check-if-a-directory-is-writeable-in-php

반응형