PHP 文件权限...让它“写入”?

发布于 2024-08-03 11:36:03 字数 1179 浏览 3 评论 0原文

我正在尝试为我一直在使用的系统编写安装脚本。该脚本将一些默认文件从一个位置复制到另一个位置,并为它们创建各种文件夹。我对此很满意,但唯一的问题是,当我通过 FTP 登录时,我无法编辑或删除 PHP 为我移动的文件。

如果我通过终端登录,我可以愉快地“sudo chmod -R 777 [dir]”,问题就消失了,所以问题是:

我在 PHP 端缺少什么?

我的权限函数如下:

function set_permissions($file) 
{
  if (file_exists($file)):
    chmod($file,0777);  
  endif;                
}

我知道将权限设置为 777 并不是 100% 理想,但我只是想在 PHP 为我移动文件后实现能够通过 FTP 编辑文件的结果。

希望我已经说得足够清楚了。现在这让我很困惑,所以感谢任何帮助:)

汤姆

编辑: 整个过程如下:(

mkdir($root_dir, 0777);
mkdir($images_dir, 0777);
if (!copy($orig_logo, $new_logo)) 
{
  echo "failed to copy $orig_logo...";
} 
  // see function above for details on set_permissions...       
  $this->set_permissions($new_logo);
}

所有路径也是正确的)

编辑: 我通过终端登录之前的文件具有以下权限:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

我登录并更改后的文件具有:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

该系统是一个内容管理系统,允许您通过管理区域编辑和删除文件,奇怪的是,这运行良好。似乎这些文件以某种方式被锁定,除了 Apache 之外的任何其他人都无法访问......但文件信息表明并非如此。很奇怪...

I am trying to write an install script for a system I have been working on. The script copies some default files from one location to another, and creates various folders for them. I have this bit working a treat, but the only problem is that when I login via FTP, I can't edit, or delete the files that PHP has moved for me.

If I login via terminal I can happily "sudo chmod -R 777 [dir]" and the problem goes away, so the question is:

What am I missing on the PHP end?

my permissions function is as follows:

function set_permissions($file) 
{
  if (file_exists($file)):
    chmod($file,0777);  
  endif;                
}

I understand it's not 100% ideal to set the permissions to 777, but I am simply trying to achieve the result of being able to edit the files via FTP, after PHP has moved them for me.

Hope I've been clear enough. This is puzzling me now so any help is appreciated :)

Tom

edit:
The whole process is as follows:

mkdir($root_dir, 0777);
mkdir($images_dir, 0777);
if (!copy($orig_logo, $new_logo)) 
{
  echo "failed to copy $orig_logo...";
} 
  // see function above for details on set_permissions...       
  $this->set_permissions($new_logo);
}

(All the paths are correct too)

edit:
The file before I login via terminal has the following permissions:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

The file after I login and change it has:

-rwxrwxrwx 1 www-data www-data 2739 2009-08-26 01:45 base.css

The system is a content management system that allows you to edit and delete files through the admin area, and strangely enough, this works well. It seems like the files are somehow locked out from anybody else other than Apache... but the file info suggests otherwise. It's odd...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-08-10 11:36:03

听起来您的目录也需要写入权限。

Sounds like your directory needs the write permissions as well.

风为裳 2024-08-10 11:36:03

您确定文件存在或者路径正确吗?

Are you sure the file exists or the path is correct?

始于初秋 2024-08-10 11:36:03

如果您可以通过终端chmod -R 777来解决问题,那么在运行chmod之前PHP设置的权限是什么??? 显然不是 777。我的猜测是你的 PHP 代码实际上并没有改变权限。

看看你的代码,如果文件不存在,你的权限更改函数可能会默默失败 - 例如,你给它提供了无效的文件名(错误的文件夹?错误的相对路径?),但你无法分辨,因为你的 < code>set_permissions() 函数太害怕警告你了。您应该按如下方式重写它:

function set_permissions($file) 
{
  if (!file_exists($file))
    throw new Exception(__FUNCTION__ . "() file doesn't exist! '$file'");
  chmod($file,0777);
  error_log("chmod 777 $file"); // debug
}

这使您可以看到发生了什么,并且您肯定会注意到文件名是否正确。

If you can chmod -R 777 via terminal to fix the problem, then what were the permissions set to by PHP before you ran chmod??? Obviously not 777. My guess is that your PHP code is not actually changing the permissions.

Lookat at your code, your permission-changing function could be failing silently if the file doesn't exist - e.g., you're giving it invalid file names (wrong folder? wrong relative path?) but you can't tell because your set_permissions() function is too scared of warning you. You should rewrite it as follows:

function set_permissions($file) 
{
  if (!file_exists($file))
    throw new Exception(__FUNCTION__ . "() file doesn't exist! '$file'");
  chmod($file,0777);
  error_log("chmod 777 $file"); // debug
}

This allows you to see what's happening, and you'll certainly notice if you haven't got your file names correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文