{"id":770,"date":"2023-11-11T10:40:41","date_gmt":"2023-11-11T02:40:41","guid":{"rendered":"https:\/\/www.sangqiao.com\/blog\/?p=770"},"modified":"2024-08-28T19:50:36","modified_gmt":"2024-08-28T11:50:36","slug":"relative-paths-in-php-ini-%e3%80%90%e5%be%88%e5%ae%9e%e7%94%a8%e3%80%91","status":"publish","type":"post","link":"https:\/\/www.sangqiao.com\/blog\/archives\/770","title":{"rendered":"Relative paths in PHP INI \u3010\u5f88\u5b9e\u7528\u3011"},"content":{"rendered":"<section class=\"post__StyledPost-sc-1w8nhbz-6 fgzJmM post\">\n<div class=\"photo-credit\">\n<p>Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@wilsonjim?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Jim Wilson<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/s\/photos\/sign?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Unsplash<\/a>.<\/p>\n<\/div>\n<p>I&#8217;ve always preferred to use relative file paths in my PHP INI, but ran into problems when running PHP from different directories. For example, a directive like\u00a0<code class=\"language-text\">error_log = \"..\/logs\/php_errors.log\"<\/code>\u00a0will resolve to different paths when invoked by\u00a0<code class=\"language-text\">\/public\/index.php<\/code>\u00a0(e.g. Zend Framework) and\u00a0<code class=\"language-text\">\/cli-config.php<\/code>\u00a0(e.g. Doctrine CLI). Some INI directives can be set at runtime by our application (<a href=\"http:\/\/www.php.net\/manual\/en\/ini.list.php\">see\u00a0<code class=\"language-text\">PHP_INI_ALL<\/code><\/a>), but many cannot. I finally found the solution with environment variables.<\/p>\n<p>In a past post,\u00a0<a href=\"http:\/\/www.wimpyprogrammer.com\/hosting-php-on-iis7\/\">&#8220;Hosting PHP on IIS7&#8221;<\/a>, I detailed my typical server configuration for PHP sites. For maximum flexibility, every site has its own PHP INI. To point PHP to its INI file, I create a FastCGI environment variable named\u00a0<a href=\"http:\/\/www.php.net\/manual\/en\/configuration.file.php\"><code class=\"language-text\">PHPRC<\/code><\/a>. The\u00a0<code class=\"language-text\">PHPRC<\/code>\u00a0variable stores the absolute path of the directory containing\u00a0<code class=\"language-text\">php.ini<\/code>. So the\u00a0<code class=\"language-text\">PHPRC<\/code>\u00a0variable contains a path like\u00a0<code class=\"language-text\">C:\\inetpub\\wwwroot\\Blog\\public<\/code>.<\/p>\n<p>Now I can simply use the PHPRC environment variable in my PHP INI. For example:<\/p>\n<div class=\"gatsby-highlight\" data-language=\"ini\">\n<pre class=\"language-ini\"><code class=\"language-ini\"><span class=\"token key attr-name\">error_log<\/span> <span class=\"token punctuation\">=<\/span> <span class=\"token value attr-value\">\"<span class=\"token inner-value\">${PHPRC}\/..\/logs\/php_errors.log<\/span>\"<\/span>\r\n<span class=\"token key attr-name\">upload_tmp_dir<\/span> <span class=\"token punctuation\">=<\/span> <span class=\"token value attr-value\">\"<span class=\"token inner-value\">${PHPRC}\/..\/temp\/uploads<\/span>\"<\/span>\r\n<span class=\"token key attr-name\">session.save_path<\/span> <span class=\"token punctuation\">=<\/span> <span class=\"token value attr-value\">\"<span class=\"token inner-value\">${PHPRC}\/..\/temp\/sessions<\/span>\"<\/span><\/code><\/pre>\n<\/div>\n<p>These paths will automatically update depending on which server runs my code. Granted, these paths are not truly relative. But as long as every server contains an absolute path to the PHP INI, I may as well reuse it.<\/p>\n<\/section>\n<footer class=\"post__PostFooter-sc-1w8nhbz-9 bYBKIb\">Published under\u00a0<a class=\"post__FooterTagLink-sc-1w8nhbz-10 dkYVOO\" href=\"https:\/\/www.wimpyprogrammer.com\/tag\/php\">PHP<\/a>,\u00a0<a class=\"post__FooterTagLink-sc-1w8nhbz-10 dkYVOO\" href=\"https:\/\/www.wimpyprogrammer.com\/tag\/server-admin\">Server Admin<\/a>\u00a0on\u00a0<time datetime=\"2013-04-11T00:00:00.000Z\">11 April, 2013<\/time>.<\/footer>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u81ea\uff1ahttps:\/\/www.wimpyprogrammer.com\/relative-paths-in-php-ini<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Photo by\u00a0Jim Wilson\u00a0on\u00a0Unsplash. I&#8217;ve always pref [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/770"}],"collection":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":2,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":803,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/posts\/770\/revisions\/803"}],"wp:attachment":[{"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sangqiao.com\/blog\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}