【问题标题】:why do i get 1 instead of the file required? [duplicate]为什么我得到 1 而不是所需的文件? [复制]
【发布时间】:2011-12-03 19:00:04
【问题描述】:

可能重复:
including php file from another server with php

我想拥有自己的错误系统,而不是出现 php 错误,例如,我需要来自另一台服务器的文件,而该服务器现在不可用

<?php
require 'http://example.com/file.php' or die ("that host is not available right now");
?>

但是我得到了

警告:require(1) [function.require]:打开流失败:没有这样的 第 5 行 C:\xampp\htdocs\index.php 中的文件或目录

致命错误:require() [function.require]:打开失败需要 '1' (include_path='.;C:\xampp\php\PEAR') 在 C:\xampp\htdocs\index.php 第 5 行

【问题讨论】:

  • 如果您不是example.com 的管理员,这是一个非常的坏主意。
  • @Pablo 不,这不是require(1) 的原因。 1 是这里的问题。

标签: php require


【解决方案1】:

这是因为require 'foo' or bar() 被解释为require ('foo' or bar())'foo' or bar() 等于 true,即 1。如果你想这样写,使用不同的括号:

(require 'http://example.com/file.php') or die ("that host is not available right now");

但是,这里根本不需要die,因为如果无法加载所需的文件,require 已经停止程序执行。只需require 'http://example.com/file.php'; 就可以了。您是否应该通过网络实际加载外部 PHP 文件是另一回事(提示:可能不是)。

【讨论】:

  • (回应已删除的评论:)不,因为require 已经停止程序执行! 失败的require 之后什么都没有。如果您想要自定义错误处理,请改写错误处理程序。
  • 我很抱歉这个愚蠢的问题(我的猜测)我该怎么做?
  • 改用 include 函数(它不会抛出致命错误)。并记住运算符的优先级。
  • @Jaison 对于一次性包含这是一个很好的解决方案。不过,对于全局“自定义错误处理程序”,他应该覆盖错误处理程序。
【解决方案2】:

问题在于运算符的优先级。 PHP 包括“或”比较的结果(真)。尝试删除它。

include('http://...') or die('error...');

它会起作用的。

【讨论】:

  • 如果我删除了或者我得到了其他错误
  • 如果你想自定义检查你的错误,使用 include 函数而不是 require 语句。如果您遇到错误,请阅读其他答案。打开 allow_url_include 指令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多