绝对路径不起作用但相对路径起作用

问题描述 投票:0回答:3

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

我正在检查文件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

为了交叉检查这个我在我的include尝试了test.php

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文件

php apache filepath
3个回答
3
投票

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

提示自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 include wrappers”,则可以使用URL指定要包含的文件(通过HTTP或其他受支持的包装器 - 请参阅支持的协议和包装器以获取协议列表)而不是本地路径名。

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


0
投票

不要忘记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;
}

0
投票

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

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

file_exists (getcwd() . "/test1.php");
© www.soinside.com 2019 - 2024. All rights reserved.