簡體   English   中英

絕對路徑不起作用,但相對路徑起作用

[英]absolute path not working but relative path working

我在此路徑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文件

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/zh/function.file-exists.php#75064

include()還支持文件包裝器(來自手冊)...

如果在PHP中啟用了“ URL包含包裝器”,則可以使用URL(通過HTTP或其他受支持的包裝器-請參見受支持的協議和包裝器以獲取協議列表)而不是本地路徑名來指定要包含的文件。

但通常,我不會在URL中包含任何內容。

問題是file_exists使用文件系統目錄路徑,而不使用URL。

如果要檢查絕對路徑,可以使用getcwd()找到它

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM