简体   繁体   English

从控制台脚本获取Web php.ini文件的价值

[英]Getting value of web php.ini file from console script

As we know for web and console, different PHP ini files are used. 我们知道对于Web和控制台,使用了不同的PHP ini文件。 For example if I run ini_get('max_execution_time') from console script, it would return 0 because console scripts generally don't have timeout. 例如,如果我从控制台脚本运行ini_get('max_execution_time') ,它将返回0因为控制台脚本通常没有超时。 But if I run same code from web, it will return a value that we have set for our website instead. 但是,如果我从Web运行相同的代码,它将返回我们为网站设置的值。

I want to run below code from console script: 我想从控制台脚本中运行以下代码:

ini_get('max_execution_time')

However this would return me value as 0 because PHP uses different php.ini file for console. 但是,这将为我返回0值,因为PHP为控制台使用了不同的php.ini文件。 What I want is when I run above code, I want it to return value of what would be set from php.ini that is supposed to be for web NOT console. 我想要的是当我运行上述代码时,我希望它返回将从php.ini中设置的值(该值应该用于Web NOT控制台)。

The reason why I need value of web php.ini for different values is because I am building console script that would check for values and notify me via email if any php.ini setting is not right for web application. 我需要将Web php.ini的值设置为不同值的原因是因为我正在构建控制台脚本,该脚本将检查值并在任何php.ini设置不适用于Web应用程序时通过电子邮件通知我。

FYI, I am using Laravel framework. 仅供参考,我正在使用Laravel框架。

Thanks for the help. 谢谢您的帮助。

You can use echo php_ini_loaded_file(); 您可以使用echo php_ini_loaded_file(); from the web to find the name of the web/apache2 ini file. 从网上找到web / apache2 ini文件的名称。 In my case, it is /usr/local/zend/etc/php.ini . 就我而言,它是/usr/local/zend/etc/php.ini

Then, in your CLI application, you can parse the ini file into an array which you can use to retrieve the data you need. 然后,在CLI应用程序中,您可以将ini文件解析为一个数组,您可以使用该数组来检索所需的数据。

<?php
$config = parse_ini_file('/usr/local/zend/etc/php.ini');

// Ouput of `var_dump($config);`
array (size=118)
  'engine' => string '1' (length=1)
  'short_open_tag' => string '1' (length=1)
  'precision' => string '14' (length=2)
  'output_buffering' => string '4096' (length=4)
  'zlib.output_compression' => string '' (length=0)
  'implicit_flush' => string '' (length=0)
  'unserialize_callback_func' => string '' (length=0)
  'serialize_precision' => string '-1' (length=2)
  'disable_functions' => string '' (length=0)
  'disable_classes' => string '' (length=0)
  'realpath_cache_size' => string '256k' (length=4)
  'zend.enable_gc' => string '1' (length=1)
  'expose_php' => string '1' (length=1)
  'max_execution_time' => string '500' (length=3)
  'max_input_time' => string '60' (length=2)
  'memory_limit' => string '256M' (length=4)
  'error_reporting' => string '32767' (length=5)
  'display_errors' => string '1' (length=1)
  'display_startup_errors' => string '1' (length=1)
  ...

echo $config['max_execution_time']; // 500

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM