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.

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

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献