dtlab08822 2017-09-05 07: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-05 07: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.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效