首页 \ 问答 \ 在Windows和CentOS上PHP的include / require的不同行为(Different behavior of PHP’s include/require on Windows and CentOS)

在Windows和CentOS上PHP的include / require的不同行为(Different behavior of PHP’s include/require on Windows and CentOS)

这些是我的PHP应用程序myApp.phpmypartsA/main.php

myApp.php

<?php
echo ("##### myApp is starting now !!! </br>");
require_once('/parts/mypartsA/main.php');
?>

mypartsA / main.php

<?php
echo ("#### mypartsA is required </br>");
?>

myApp.phpmypartsA/main.php的结构如下。

Document_root 
  |
  +--- test
  |     |
  |     +--- myApp.php
  |
  +--- parts
        |
        +--- mypartsA
               |
               +---main.php

我已经在Windows + Apache上使用了这个应用程序一年没有问题但最近我将其迁移到CentOS + Apache然后我得到与include_path相关的致命错误

PHP Warning:  require_once(/parts/mypartsA/main.php) 
[<a href='function.require-once'>function.require-once</a>]
: failed to open stream
: No such file or directory 
in /var/www/html/test/myApp.php on line 12 
PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]
: Failed opening required (/parts/mypartsA/main.php’
(include_path='.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html') 
in /var/www/html/test/myAPP.php on line 12

根据PHP.net,如果定义了路径,则include_path将被忽略。 http://www.php.net/manual/en/function.include.php

我认为在我的情况下,PHP尝试从C:\/而不是从include_path查找parts目录。

require_once(/parts/mypartsA/main.php);

因此,无论是在Windows上还是在CentOS上, myAPP.php结果myAPP.php必须是致命的错误。

但我的应用程序已经工作了一年,在Windows上没有错误。 PHP可以正确找到parts目录。

可以在Windows上尝试上面的代码,看看..

##### myApp is starting now !!! 
#### mypartsA is required

或者得到错误?

现在,我想知道为什么在Windows上运行和在CentOS上运行相同代码的结果是不同的。

我必须检查PHP.ini中的什么指令?

我知道如果我更改此代码以使用$ _SERVER['DOCUMENT_ROOT']添加绝对路径。

require_once($_SERVER['DOCUMENT_ROOT']./parts/mypartsA/main.php);

它会很好用。 但是有许多应用程序具有相同的编码风格,我不想改变所有这些应用程序。

这些是其他信息。

include_path值phpinfo()在Windows上显示

.;C:\PHP;C:\PHP\pear;C:\Apache2.2.22\htdocs

include_path值phpinfo()在CentOS上显示

.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html

These are code of my PHP applications myApp.phpand mypartsA/main.php.

myApp.php

<?php
echo ("##### myApp is starting now !!! </br>");
require_once('/parts/mypartsA/main.php');
?>

mypartsA/main.php

<?php
echo ("#### mypartsA is required </br>");
?>

And myApp.php and mypartsA/main.php are in following structure.

Document_root 
  |
  +--- test
  |     |
  |     +--- myApp.php
  |
  +--- parts
        |
        +--- mypartsA
               |
               +---main.php

I’ve used this application on Windows+Apache for a year with no problem but recently I migrate this to CentOS+Apache then I get fatal error related to include_path

PHP Warning:  require_once(/parts/mypartsA/main.php) 
[<a href='function.require-once'>function.require-once</a>]
: failed to open stream
: No such file or directory 
in /var/www/html/test/myApp.php on line 12 
PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]
: Failed opening required (/parts/mypartsA/main.php’
(include_path='.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html') 
in /var/www/html/test/myAPP.php on line 12

According to PHP.net if a path is defined the include_path will be ignored. http://www.php.net/manual/en/function.include.php

I think in my case PHP tries to find parts directory from C:\ or / not from include_path.

require_once(/parts/mypartsA/main.php);

Therefore result of myAPP.php must be fatal error whether on Windows or on CentOS.

But my application has been working for a year with no error on Windows. PHP can find parts directory correctly.

Can sameone try those codes above on Windows and see..

##### myApp is starting now !!! 
#### mypartsA is required

or get error?

Now,I want to know is why results of same code are different between running on Windows and running on CentOS.

What dirctive in PHP.ini do I have to check ?

I know if I change this code to add absolute path with $ _SERVER['DOCUMENT_ROOT'].

require_once($_SERVER['DOCUMENT_ROOT']./parts/mypartsA/main.php);

It’ll work well. But there are many applications with same style of coding and I don’t want to change all of them.

These are additional information.

include_path values phpinfo() shows on Windows

.;C:\PHP;C:\PHP\pear;C:\Apache2.2.22\htdocs

include_path values phpinfo() shows on CentOS

.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html

原文:https://stackoverflow.com/questions/15539793
更新时间:2024-01-06 11:01

最满意答案

如果您的表单数据是通过name属性如user[profile_attributes][gender] (均具有user前缀)的字段提交的,则可以检查:user存在于params

... if params.include?(:user)

如果由于某种原因(例如来自路由), params[:user]即使对于GET请求也将具有值,那么您可以查找具有值的特定表单字段。 例如,您可以添加一个隐藏字段

<%= f.hidden_field :some_field, :value => true %>

并检查你的情况

... if params[:user].include?(:some_field)

您可以选择是否通过POST方法检查请求

... if request.post?

这也适用于其他方法,如request.put? 用于更新方法。


If your form data is submitted through fields with name attributes like user[profile_attributes][gender] (all having the user prefix), you can check if the :user exists in params.

... if params.include?(:user)

If for some reason (like coming from the route) params[:user] is already going to have a value even for GET requests, you can look for a specific form field having a value. For example, you could add a hidden field

<%= f.hidden_field :some_field, :value => true %>

and check for it in your condition

... if params[:user].include?(:some_field)

You can alternatively check if the request is via the POST method

... if request.post?

This works for other methods as well, like request.put? for an update method.

相关问答

更多
  • 就在这儿: var test = localStorage.input === 'true'? true: false; $('input').prop('checked', test); 就是为什么所有复选框都被检查。 你有一个存储在localStorage.input的全局变量。 当您调用$('input').prop('checked', test) ,将选择所有输入并应用该属性。 将状态存储在localStorage似乎是一种明智的做法,但您需要单独为每个复选框存储状态,并且需要将这些属性 ...
  • 这是错误的: def create @Event = Event.new(params[:Event]) .... end 它应该是: def create @Event = Event.new(params[:event]) .... end HF ... //并且你也应该在cvariable Names中使用小写字母。 大写只能用于常量,比如ClassName! This is wrong: def create @Event = Event.new(para ...
  • 它看起来像路由可能会搞砸了? 你确定search行动实际上被称为? 它看起来会用id search show 。 尝试添加debugger调用或Rails.logger.debug调用以查看正在调用的操作。 或者只是看看整个日志条目。 It looks like the routing might be messed up? Are you sure the search action is actually being called? It looks like it's going to show wi ...
  • 如果您的表单数据是通过name属性如user[profile_attributes][gender] (均具有user前缀)的字段提交的,则可以检查:user存在于params 。 ... if params.include?(:user) 如果由于某种原因(例如来自路由), params[:user]即使对于GET请求也将具有值,那么您可以查找具有值的特定表单字段。 例如,您可以添加一个隐藏字段 <%= f.hidden_field :some_field, :value => true %> 并检查 ...
  • 你看过TinyMCE吗? 它配置非常简单,您只需传递相应的参数即可包含您需要的选项。 它甚至不需要任何宝石或js库。 详细信息 - http://www.tinymce.com/ Have you looked at TinyMCE ? It's pretty easy to configure and you can just pass corresponding parameters to include those options that you require. It does not not e ...
  • 我没有使用calendar_date_select插件,但是您应该已经获取参数。 params[:date_range][:start] params[:date_range][:end] I haven't used the calendar_date_select plugin, but you should be getting the parameters back already. params[:date_range][:start] params[:date_range][:end]
  • 把这样的东西添加到你的application_controller.rb : before_action :store_ref private def store_ref cookies[:ref] = params[:ref] if params[:ref].present? end 像这样的东西给你的用户创建操作: def create @user = User.new(user_params) @user.ref = cookies[:ref] if @user.save ...
  • 你提到的两个框架都可以处理满足这些要求的网站。 您还应该将Django和Symfony添加到您可能选择的列表中,因为它们也可以满足您的需求。 你有使用Ruby / PHP / Python构建应用程序的经验吗? Both of the frameworks you mention could handle a web site with those requirements. You should also add Django and Symfony to your list of possible ch ...
  • instantrails应该是你正在寻找的。 http://rubyforge.org/frs/?group_id=904 instantrails should be what you're looking for. http://rubyforge.org/frs/?group_id=904
  • 看起来你需要改变你选择使用options_for_select标签: <%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, c.id ] }), :include_blank => true %> 如果你想预先填充category_id <%= f.select :category_id, options_for_select(Category.all.collect {|c| [ c.name, ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)