dsaff82024 2015-09-17 15:37
浏览 86
已采纳

尝试在public_html中创建页面时,错误无法打开流权限被拒绝

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); 
fclose($fp); 
chmod($page_path.$loop[Html_Name], 0666);

Many thanks in advance.

  • 写回答

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!'; 
    }
    ftp_close($connection);
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试