dtlab08822 2017-09-04 23:12
浏览 130
已采纳

如何在php中打开linux文件?

I'm trying to make an installer for my PHP project. I need to add the following block into <VirtualHost *:80>. Here is a part of my installer script:

<?php

$path = "/var/www/html/myProject";

$default_conf = shell_exec('cat /etc/apache2/sites-available/000-default.conf');

$new_directory = "<Directory $path/public>
                     Allow Override all
                     Require all granted
                  </Directory>";

$res = str_replace("</VirtualHost>","$new_directory</VirtualHost>", $default_conf);

file_put_contents("/etc/apache2/sites-available/000-default.conf",$res);

But $default_conf is empty. Why? How can I fix it?


Noted that the result of this command in the command line environment

cat /etc/apache2/sites-available/000-default.conf

is the content of 000-default.conf file which is exactly as expected (it is not empty).

  • 写回答

1条回答 默认 最新

  • dongyan8929 2017-09-04 23:20
    关注

    You probably have a permissions problem with running the shell command.

    Why not try the function file_get_contents which is simpler, safer and cross platform.

    http://php.net/manual/en/function.file-get-contents.php

    Further, you can use file_exists and is_readable to double check everything and help with debugging.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部