파일 권한

파일 권한

심볼릭 링크를 생성하는 PHP 스크립트를 실행하고 있습니다.

어떤 사용자인지 확인하려면:

file_put_contents("testFile", "test");
$user = fileowner("testFile");
unlink("testFile");
echo "running as user '" . $user . "'";
var_dump( exec('whoami'));

이렇게 달려라...

$ php script.php

올바르게 실행되고 모든 심볼릭 링크가 설정되며 출력은 다음과 같습니다.

running as user '999'string(5) "admin"

쉘 스크립트를 통해 실행:

#!/bin/sh
php /path/to/script.php

다음과 같은 출력을 제공하지만 작동하지 않습니다.

PHP 경고: Symlink(): 8행의 /path/to/script.php에서 권한이 거부되었습니다. 사용자 '999' string(5) 'admin'으로 실행 중입니다.

동일한 사용자로 실행되기 때문에 둘 사이의 차이점이 무엇인지 잘 모르겠습니다.

올바른 심볼릭 링크 권한을 모두 갖도록 하는 방법에 대한 제안 사항이 있습니까?


cat /proc/version

다음을 제공합니다:

Linux version 2.6.39 (root@cross-builder) (gcc version 4.6.3 (x86 32-bit toolchain - ASUSTOR Inc.) ) #1 SMP PREEMPT Thu Oct 31 21:27:37 CST 2013

이는 모든 종류의 릴리스 정보에 대해 생성할 수 있는 유일한 출력입니다.


모든 코드:

$files = scandir('/volume1/dir1');
$base = "/volume1/";
foreach($files as $file) {
        $f_letter = $file{0};
        $new_path = $base . "ByLetter/" . strtoupper($f_letter) . "/" . $file;
        if(ctype_alpha ($f_letter) && !is_link($new_path)) {
                var_dump($base. "TV/" . $file);
                var_dump($new_path);
                symlink ($base . "TV/" . $file , $new_path);
        }


}

두 방법의 var 덤프는 동일한 출력을 제공합니다.

답변1

절대 경로를 사용해 보세요. 이 코드는 unlink("testFile");현재 작업 디렉터리에서 파일을 찾습니다. pwd는 현재 작업 디렉터리에 따라 변경됩니다. 그래서 사용unlink("/path/to/testFile");

답변2

게시한 코드를 가져와 유용한 기능의 시작이라고 생각하고 수정했지만 실패할 수 없었습니다(아마도 내 사용자가 두 경로 모두에 대한 쓰기 액세스 권한을 갖고 있기 때문일 것입니다). 문제가 발생하면 알려드리기 위해 많은 검사를 추가했습니다. 여전히 같은 문제가 발생하면 알려주시기 바랍니다. CLI를 통해 스크립트를 실행하면 해당 스크립트는 자신과 동일한 권한을 가지지만, 웹 서버를 통해 실행하면 웹 서버 사용자의 권한(예: www-data) 을 갖게 된다는 점을 기억하는 것이 중요합니다.

<?php

/**
 * Creates an index of the files in the specified directory, by creating symlinks to them, which
 * are separated into folders having the first letter.
 * WARNING - this will only index files that start with alphabetical characters.
 * @param $directory_to_index - the directory we wish to index.
 * @param $index_location - where to stick the index.
 * @return void
 */
function create_file_index($directory_to_index, $index_location)
{
    # Don't let the user place the index in the same folder being indexed, otherwise the directory
    # cannot be re-indexed later, otherwise we will be indexing the index.
    if ($directory_to_index == $index_location)
    {
        throw new Exception('Cannot place index in same folder being indexed!');
    }

    # delete the old index if one already exists.
    if (file_exists($index_location))
    {
        deleteNonEmptyDir($index_location);
    }

    if (!mkdir($index_location))
    {
        throw new Exception('Failed to create index directory, check write permissions');
    }

    $files = scandir($directory_to_index);

    foreach ($files as $filename) 
    {
        $first_letter = $filename[0];
        $placement_dir = $index_location . "/" . strtoupper($first_letter);

        if (ctype_alpha($first_letter))
        {
            # create the placement directory if it doesn't exist already
            mkdir($placement_dir);

            $new_path = $placement_dir . "/" . $filename;

            if (!is_link($new_path)) 
            {
                symlink($directory_to_index . '/' . $filename, $new_path);
            }
        }
    }
}

/**
 * Deletes a directory even if it is not already empty. This resolves the issue with
 * trying to use unlink on a non-empty dir.
 * @param String $dir - the path to the directory you wish to delete
 * @return void - changes your filesystem
 */
function deleteNonEmptyDir($dir) 
{
    if (is_dir($dir)) 
    {
        $objects = scandir($dir);

        foreach ($objects as $object) 
        {
            if ($object != "." && $object != "..") 
            {
                if (filetype($dir . "/" . $object) == "dir")
                {
                    deleteNonEmptyDir($dir . "/" . $object); 
                }
                else
                {
                    unlink($dir . "/" . $object);
                }
            }
        }

        reset($objects);
        rmdir($dir);
    }
}

create_file_index('/volume1/dir1', '/volume1/dir1/ByLetter');

답변3

PHP 함수fileowner반환숫자 UID사용자 이름 대신. uid 999는 아마도 귀하의 admin사용자에 해당할 것입니다. 다음 명령을 사용하여 확인할 수 있습니다 id.

id admin

출력은 계정의 uid로 시작되어야 합니다.

관련 정보