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:

  1. <?php
  2. $path = "/var/www/html/myProject";
  3. $default_conf = shell_exec('cat /etc/apache2/sites-available/000-default.conf');
  4. $new_directory = "<Directory $path/public>
  5. Allow Override all
  6. Require all granted
  7. </Directory>";
  8. $res = str_replace("</VirtualHost>","$new_directory</VirtualHost>", $default_conf);
  9. 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.

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

报告相同问题?

悬赏问题

  • ¥20 游戏mod是如何制作的
  • ¥15 关于#hadoop#的问题:按照老师上课讲的步骤写的
  • ¥20 有人会用这个工具箱吗 付fei咨询
  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
  • ¥15 opencv打开dataloader显示为nonetype
  • ¥15 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
手机看
程序员都在用的中文IT技术交流社区

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

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

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

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

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

客服 返回
顶部