【问题标题】:absolute path not working but relative path working绝对路径不工作,但相对路径工作
【发布时间】:2018-06-04 21:24:13
【问题描述】:

我在这条路径http://172.16.15.11/appointment/src/上有两个文件test.phptest1.php

我正在检查文件test1.php 是否存在,但是当我提供absolute path 时它不起作用:

我在test.php中有以下代码

//giving absolute path not working
var_dump(file_exists('http://172.16.15.11/appointment/src/test1.php')); //return false

//but when i give relative path it does work
var_dump(file_exists('test1.php')); //return true

为了交叉检查,我在test.php 中尝试了include

include('http://172.16.15.11/appointment/src/test1.php'); //but in this case absolute path work

如果我在我给出绝对路径时它的工作原理

1. include('http://172.16.15.11/appointment/src/test1.php'); 

2. header('location:http://172.16.15.11/appointment/src/test1.php');

但是当我检查这个文件时不起作用:

var_dump(file_exists('http://172.16.15.11/appointment/src/test1.php')); //return false

注意 - 我没有任何 .htaccess 文件

【问题讨论】:

  • http://172.16.15.11/appointment/src/test1.php 不是路径,而是 URL。
  • 还有 var_dump(file_exists('appointment/src/test1.php'));不起作用,但 var_dump(file_exists('/var/www/html/appointment/src/test1.php'));作品

标签: php apache filepath


【解决方案1】:

问题是 file_exists 使用文件系统目录路径,而不是 URL。

如果你想检查绝对路径,你可以使用getcwd()找到它

file_exists (getcwd() . "/test1.php");

【讨论】:

  • 我试过 var_dump(file_exists('appointment/src/test1.php'));但它不工作......它只在 var_dump(file_exists('test1.php'));
  • 因为约会不在您的根文件系统结构中。您可以使用 getcwd() 打印当前目录。
  • 非常感谢使用 getcwd() 后返回 /var/www/html/appointment/src var_dump(file_exists('/var/www/html/appointment/src/test1.ph‌​p')) ;它有效....但是 var_dump(file_exists('appointment/src/test1.php'));不工作是什么原因
  • 因为在您提供给函数的相对路径中找不到该文件。 file_exists('appointment/src/test1.php') 与写 file_exists('/var/www/html/appointment/src/appointment/src/test1.php') 和路径 /var/www/html 相同/appointment/src/appointment/src/test1.php 没有任何文件...
【解决方案2】:

不要忘记 301 等...仅正确 200... 如果您不想在错误日志中显示通知,请始终检查 isset。

$headers = @get_headers('http://www.examle.com/somefile.jpg');
if(isset($headers[0]) AND $headers[0] == 'HTTP/1.1 200 OK') {
    $exists = true;
}
else {
    $exists = false;
}

【讨论】:

    【解决方案3】:

    file_exists() 可能适用于某些 URL,但不能保证,从手册页...

    Tip 从 PHP 5.0.0 开始,这个函数也可以和一些 URL 一起使用 包装纸。请参阅支持的协议和包装器以确定哪些 包装器支持 stat() 系列功能。

    你可以试试……

    $file = 'http://www.examle.com/somefile.jpg';
    $file_headers = @get_headers($file);
    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
        $exists = false;
    }
    else {
        $exists = true;
    }
    

    (来自http://www.php.net/manual/en/function.file-exists.php#75064

    include() 还支持文件包装器(来自手册)...

    如果“网址 包含包装器”在 PHP 中启用,您可以指定文件为 使用 URL 包含(通过 HTTP 或其他受支持的包装器 - 请参阅 支持的协议和包装器用于协议列表)而不是 本地路径名。

    但作为一项规则,我不会包含 URL 中的任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2022-01-14
      • 2012-01-11
      • 2010-09-15
      • 2013-04-17
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多