2017-09-05 56 views
1

我想爲我的PHP項目做一個安裝程序。我需要將以下塊添加到<VirtualHost *:80>。以下是我的安裝程序腳本的一部分:如何在php中打開一個linux文件?

<?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); 

但是$default_conf爲空。爲什麼?我該如何解決它?


注意,在命令行環境

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

此命令的結果是000-default.conf文件而這正是如預期(它不是空的)的內容。

+1

您是否嘗試過'error_reporting(E_ALL); ini_set('display_errors',1);'在頂部 - 您可能會收到'Permission denied'消息。 –

+0

@NigelRen我的腳本名稱是'installer.php',我已經運行了這個命令:'sudo chmod 777 installer.php' – stack

+0

看起來像web服務器用戶沒有權限做,或者你可能需要絕對路徑爲你的' cat'命令 –

回答

0

您可能在運行shell命令時遇到權限問題。

爲什麼不嘗試功能file_get_contents這是更簡單,更安全和跨平臺。

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

此外,您可以使用file_existsis_readable仔細檢查一切和調試幫助。

+0

你說得對。使用'file_get_contents()'似乎更簡潔https://stackoverflow.com/a/3575012/5259594 – stack