PHP에 디렉토리에 대한 쓰기 권한을 어떻게 부여합니까?
PHP를 사용하여 파일을 만들려고하는데 작동하지 않습니다. 나는 이것이 쓰기 권한이 없기 때문이라고 가정합니다 (항상 이전에 문제였습니다). chmod 0777 폴더를 만들어 이것이 문제인지 테스트하려고했지만 그 디렉토리의 모든 스크립트가 다시 변경 될 때까지 500 오류 메시지를 반환하게되었습니다. 파일을 생성 할 수 있도록 파일 시스템에 대한 PHP 쓰기 액세스 권한을 어떻게 부여합니까?
편집 : Apache를 사용하여 Hostgator 공유 호스팅에서 호스팅됩니다.
편집 2 : 누군가 코드를 요청했습니다. 코드는 GD 이미지 스크립트입니다. 나는 그것이 호출되는 모든 이미지를 이전에 만들었던 것처럼 나머지가 작동한다는 것을 알고 있습니다. 이제 새 텍스트가 추가 될 때 만들어서 폴더에 저장하려고합니다. 내가 가진 쓰기 줄은 다음과 같습니다. imagejpeg (null, $ file, 85);
또한 깨진 스크립트 (주로 tizag에서 복사)인지 확인하기 위해 테스트 파일을 만들었습니다. http://gearboxshow.info/rkr/lesig.jpg/testfile.txt (게시 방법 / 게시 방법을 모릅니다. 여기에 코드가 올바르게 표시됩니다. 여기에 PHP 스크립트의 내용이 있고 PHP 태그가 없습니다.)
13,13,1 (별도의 줄)을 반환하므로 무언가를 썼다고 생각하는 것처럼 보이지만 testfile.txt는 비어 있거나 (빈 파일을 업로드했습니다) 존재하지 않습니다 (삭제하면).
편집 3 : 서버가 CentOS를 실행합니다.
쉬운 방법은 PHP가 처음에 디렉토리 자체를 생성하도록하는 것입니다.
<?php
$dir = 'myDir';
// create new directory with 744 permissions if it does not exist yet
// owner will be the user/group the PHP script is run under
if ( !file_exists($dir) ) {
mkdir ($dir, 0744);
}
file_put_contents ($dir.'/test.txt', 'Hello File');
이렇게하면 권한이있는 번거 로움을 덜 수 있습니다.
디렉토리의 소유자를 아파치를 실행하는 사용자로 설정합니다. 종종 리눅스에 아무도 없다
chown nobody:nobody <dirname>
이렇게하면 폴더는 쓰기 가능하지 않지만 아파치에 대해 쓰기 가능합니다. :)
간단한 3 단계 솔루션
요약 : 디렉토리 소유자를 PHP가 사용하는 사용자 (웹 서버 사용자)로 설정해야합니다 .
1 단계 : PHP 사용자 결정
다음을 포함하는 PHP 파일을 만듭니다.
<?php echo `whoami`; ?>
웹 서버에 업로드하십시오. 출력은 다음과 유사해야합니다.
www-data
따라서 PHP 사용자는 www-data
.
2 단계 : 디렉터리 소유자 결정
다음으로 명령 줄을 통해 웹 디렉토리의 세부 정보를 확인합니다.
ls -dl /var/www/example.com/public_html/example-folder
결과는 다음과 유사해야합니다.
drwxrwxr-x 2 exampleuser1 exampleuser2 4096 Mar 29 16:34 example-folder
따라서 디렉토리 소유자는 exampleuser1
입니다.
3 단계 : 디렉토리 소유자를 PHP 사용자로 변경
그런 다음 웹 디렉터리의 소유자를 PHP 사용자로 변경합니다.
sudo chown -R www-data /var/www/example.com/public_html/example-folder
웹 디렉토리의 소유자가 변경되었는지 확인하십시오.
ls -dl /var/www/example.com/public_html/example-folder
결과는 다음과 유사해야합니다.
drwxrwxr-x 2 www-data exampleuser2 4096 Mar 29 16:34 example-folder
따라서의 소유자가 example-folder
PHP 사용자로 성공적으로 변경되었습니다 : www-data
.
Done! PHP should now be able to write to the directory.
1st Figure out which user is owning httpd process using the following command
ps aux | grep httpd
you will get a several line response like this:
phpuser 17121 0.0 0.2 414060 7928 ? SN 03:49 0:00 /usr/sbin/httpd
Here 1st column shows the user name. So now you know the user who is trying to write files, which is in this case phpuser
You can now go ahead and set the permission for directory where your php script is trying to write something:
sudo chown phpuser:phpuser PhpCanWriteHere
sudo chmod 755 PhpCanWriteHere
You can change the permissions of a folder with PHP's chmod(). More information on how to use the command is here: http://php.net/manual/en/function.chmod.php
If you get a 500 Error when setting the permissions to 777 (world writable), then it means your server is setup to prevent executing such files. This is done for security reasons. In that case, you will want to use 755 as the highest permissions on a file.
If there is an error_log file that is generated in the folder where you are executing the PHP document, you will want to view the last few entries. This will give you an idea where the script is failing.
For help with PHP file manipulation, I use http://www.tizag.com/phpT/filewrite.php as a resource.
I found out that with HostGator you have to set files to CMOD 644 and Folders to 755. Since I did this based on their tech support it works with HostGator
You can set selinux to permissive in order to analyze.
# setenforce 0
Selinux will log but permit acesses. So you can check the /var/log/audit/audit.log
for details. Maybe you will need change selinux context. Fot this, you will use chcon
command. If you need, show us your audit.log
to more detailed answer.
Don't forget to enable selinux after you solved the problem. It better keep selinux enforced.
# setenforce 1
Best way in giving write access to a directory..
$dst = "path/to/directory";
mkdir($dst);
chown($dst, "ownername");
chgrp($dst, "groupname");
exec ("find ".$dst." -type d -exec chmod 0777 {} +");
chmod does not allow you to set ownership of a file. To set the ownership of the file you must use the chown command.
I had the same problem:
As I was reluctant to give 0777 to my php directory, I create a tmp directory with rights 0777, where I create the files I need to write to.
My php directory continue to be protected. If somebody hackes the tmp directory, the site continue to work as usual.
I'm running Ubuntu, and as said above nobody:nobody does not work on Ubuntu. You get the error:
chown: invalid group: 'nobody:nobody'
Instead you should use the 'nogroup', like:
chown nobody:nogroup <dirname>
참고URL : https://stackoverflow.com/questions/2900690/how-do-i-give-php-write-access-to-a-directory
'Nice programing' 카테고리의 다른 글
인터페이스 메서드에 본문이있을 수 있습니까? (0) | 2020.11.14 |
---|---|
파이썬은 적절한 줄 끝을 얻습니다. (0) | 2020.11.14 |
iPhone의 ALAsset에서 가져온 URL의 이미지 표시 (0) | 2020.11.14 |
인수 배열이 주어지면 해당 인수를 Ruby의 특정 함수에 어떻게 보내나요? (0) | 2020.11.14 |
단일 고유 열을 기반으로 고유 행 선택 (0) | 2020.11.14 |