2015-09-17 15:37
浏览 80


I am trying to run a script which creates pages and saves them to the server but am getting a permission error on one of the files that is in the public_html directory.

So 2 pages are created in the "pages" directory which is chmod to 0777 and they are created fine. The 3rd page is created in the "public_html" directory which fails with you do not have permission. The only way i have found to fix this is to chmod the "public_html" directory to 0770 which then everything works but i have been strongly advised by the hosting company not to do this bevause of the security risk.

So my question is, Is there any otherway to achieve this goal? Looking into it a bit it looks like i need to give the script "user" priviliges might work but this is beyond my knowledge at the moment. I`m not even sure what the script is running as at the moment, I would guess "group" as chmoding the public_html to 0770 allows "group"

My setup is: vps server running centos CENTOS 6.7 x86_64

php 5, dso, Apache suEXEC on

simplified Code i am using is:

$page_path = "/home/username/public_html/";
$loop[Html_Name] = "test.html";
$new_page_file = "test.html";

$fp = fopen($page_path.$loop[Html_Name], "w"); 
fwrite($fp, $new_page_file); 
chmod($page_path.$loop[Html_Name], 0666);

Many thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试运行一个创建页面并将其保存到服务器的脚本 但是我收到了权限错误 public_html目录中的一个文件。

因此在“pages”目录中创建了2个页面,chmod为0777并且它们被正确创建。 第3页创建 在“public_html”目录中失败但你没有权限。 我找到修复此问题的唯一方法是将“public_html”目录chmod到0770然后 everything工作但我得到了托管公司的强烈建议 因为存在安全风险,所以不要这样做。

所以我的问题是,有没有其他方法可以实现这个目标? 稍微看一下它看起来我需要给出脚本 “用户”权限可能有效,但是目前我还不知道。 我甚至不确定脚本运行的是什么,我猜“组”作为chmoding public_html to 0770允许“组” “

我的设置是:vps服务器运行centos CENTOS 6.7 x86_64

php 5,dso,Apache suEXEC on

简化 我正在使用的代码是:

  $ page_path =“/ home / username / public_html /”; 
 $ loop [Html_Name] =“test.html”; 
 $  new_page_file =“test.html”; 
 $ fp = fopen($ page_path。$ loop [Html_Name],“w”);  
fwrite($ fp,$ new_page_file);  
fclose($ FP);  
chmod($ page_path。$ loop [Html_Name],0666); 


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpw50696 2015-09-17 21:57

    Typically, we use ftp in these situations. /public_html permissions may remain to 750 and run this code.

    $server = 'localhost';
    $ftp_user_name = 'username';
    $ftp_user_pass = 'passw';
    $dest = 'public_html/new.file';
    $source = '/home/username/public_html/path/to/existing.file';
    $connection = ftp_connect($server);
    $login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
    if (!$connection || !$login) { die('Ftp not connected.'); }
    $copied = ftp_put($connection, $dest, $source, FTP_BINARY);
    if ($copied) { 
        echo 'File copied';
    } else {
        echo 'Copy failed!'; 

    The page with final destination in public_html can be created in the other directory and then this script will copy it in public_html. The old file will remain and if a file exists with the same destination name will be overwritten.
    The $dest is relative path to user home directory. The $source is absolute path.
    The connection will fail if the ftp is concurrently used by filezilla or something. A solution to that is to create a second ftp user account in cPanel.

    打赏 评论

相关推荐 更多相似问题