使用 PHP 临时将文件夹权限更改为 777

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15219007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:48:53  来源:igfitidea点击:

Change folder permission to 777 using PHP temporarily

phpfolder-permissions

提问by Sumit Bijvani

I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.

我的服务器上有一个具有 755 权限的 Video 文件夹。问题是:当有人上传视频文件时,由于权限错误而无法上传到该文件夹​​中。

If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.

如果我将权限更改为 777,则可以上传视频。但出于安全原因,我不想允许文件夹权限为 777。

Is there any way in PHP to temporary change the permission to 777 while uploading video?

PHP 有什么办法可以在上传视频时临时将权限更改为 777?

回答by hjpotter92

PHP provides a function, chmod()for the task.

PHP 提供了一个函数,chmod()用于任务。

Attempts to change the mode of the specified file to that given in mode.

尝试将指定文件的模式更改为 mode 中给定的模式。

You can put it in an ifstatement, and if it returns false, you can skip the upload file part.

可以放在if语句中,如果返回false,可以跳过上传文件部分。



The usage will be like

用法会像

if( chmod($path, 0777) ) {
    // more code
    chmod($path, 0755);
}
else
    echo "Couldn't do it.";

As described in the chmod function manual, the $modemust be in octal format - with leading zero, i.e chmod($path, 0777)

chmod函数手册中所述,$mode必须是八进制格式 - 前导零,即chmod($path, 0777)

回答by Maxim Krizhanovsky

There is a way (PHP provides chmodfunction) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.

有一种方法(PHP 提供了chmod功能),但由于 PHP 不是文件夹的所有者,因此您将无法更改权限。而且我认为您正在解决错误的问题。在同一组中添加 webserver 和 PHP,并将 775 添加到文件夹中。

回答by Edwin Alex

You can use chmod()function.

您可以使用chmod()功能。

For more information, try here

有关更多信息,请尝试这里

回答by Viswanath Vichu R

You have to initialize the config for the upload, like this:

您必须为上传初始化配置,如下所示:

$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);