I need to check if a particular file exists on a remote server. Using is_file()
and file_exists()
doesn't work. Any ideas how to do this quickly and easily?
我需要检查远程服务器上是否存在特定文件。使用is_file()和file_exists()不起作用。任何想法如何快速,轻松地做到这一点?
6 个解决方案
#1
56
you have to use CURL
你必须使用CURL
function is_url_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code == 200){
$status = true;
}else{
$status = false;
}
curl_close($ch);
return $status;
}
#2
48
You don't need CURL for that... Too much overhead for just wanting to check if a file exists or not...
你不需要CURL ...只是想要检查一个文件是否存在太多开销......
Use PHP's get_header.
使用PHP的get_header。
$headers=get_headers($url);
Then check if $result[0] contains 200 OK (which means the file is there)
然后检查$ result [0]是否包含200 OK(这意味着文件在那里)
A function to check if a URL works could be this:
检查URL是否有效的函数可能是这样的:
function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
/* You can test a URL like this (sample) */
if(UR_exists("http://www.amazingjokes.com/"))
echo "This page exists";
else
echo "This page does not exist";
#3
13
I've just found this solution:
我刚刚找到了这个解决方案:
if(@getimagesize($remoteImageURL)){
//image exists!
}else{
//image does not exist.
}
Source: http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/
资料来源:http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/
#4
4
Hi according to our test between 2 different servers the results are as follows:
嗨,根据我们在2个不同服务器之间的测试,结果如下:
using curl for checking 10 .png files (each about 5 mb) was on average 5.7 secs. using header check for the same thing took average of 7.8 seconds!
使用curl检查10个.png文件(每个约5 mb)平均为5.7秒。使用标题检查同样的事情平均需要7.8秒!
So in our test curl was much faster if you have to check larger files!
所以在我们的测试中,如果你需要检查更大的文件,卷曲会更快!
our curl function is:
我们的卷曲功能是:
function remote_file_exists($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( $httpCode == 200 ){return true;}
return false;
}
here is our header check sample:
这是我们的标题检查示例:
function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
#5
1
Do a request with curl and see if it returns a 404 status code. Do the request using the HEAD request method so it only returns the headers without a body.
使用curl执行请求并查看它是否返回404状态代码。使用HEAD请求方法执行请求,以便它只返回没有正文的标题。
#6
0
There is another Skywalker
For anyone searching and not wanting to make requests to the file, there is a way! Simply use $_SERVER['DOCUMENT_ROOT']
and the relative path from the url (using parse_url
) and then check with file_exists
.
对于任何搜索并且不想对文件发出请求的人,都有办法!只需使用$ _SERVER ['DOCUMENT_ROOT']和url的相对路径(使用parse_url),然后使用file_exists进行检查。
$_SERVER['DOCUMENT_ROOT']
does not guarantee having a/no trailing slash, though, so I have accounted for this in my example below.
但是,$ _SERVER ['DOCUMENT_ROOT']不保证有/不带尾随斜杠,所以我在下面的例子中说明了这一点。
Useful links
$_SERVER indices (see 'DOCUMENT_ROOT')
parse_url
file_exists
$ _SERVER索引(参见'DOCUMENT_ROOT')parse_url file_exists
Example
// Your URL here!
$my_url = 'https://*.com/opensearch.xml';
// Get document root
$doc_root = $_SERVER['DOCUMENT_ROOT'];
// Account for possible trailing slash
if( substr( $doc_root, strlen( $doc_root )-1, 1 ) == '/' ){
$doc_root = substr( $doc_root, 0, strlen( $doc_root - 1 ) );
}
// File location
$file = $doc_root . parse_url( $my_url )['path'];
// Update the src in the global if the child file exists
if( file_exists( $file ) ){
// You've got yourself a file
}else{
// File? What file? There's no file here...
}
#1
56
you have to use CURL
你必须使用CURL
function is_url_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code == 200){
$status = true;
}else{
$status = false;
}
curl_close($ch);
return $status;
}
#2
48
You don't need CURL for that... Too much overhead for just wanting to check if a file exists or not...
你不需要CURL ...只是想要检查一个文件是否存在太多开销......
Use PHP's get_header.
使用PHP的get_header。
$headers=get_headers($url);
Then check if $result[0] contains 200 OK (which means the file is there)
然后检查$ result [0]是否包含200 OK(这意味着文件在那里)
A function to check if a URL works could be this:
检查URL是否有效的函数可能是这样的:
function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
/* You can test a URL like this (sample) */
if(UR_exists("http://www.amazingjokes.com/"))
echo "This page exists";
else
echo "This page does not exist";
#3
13
I've just found this solution:
我刚刚找到了这个解决方案:
if(@getimagesize($remoteImageURL)){
//image exists!
}else{
//image does not exist.
}
Source: http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/
资料来源:http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/
#4
4
Hi according to our test between 2 different servers the results are as follows:
嗨,根据我们在2个不同服务器之间的测试,结果如下:
using curl for checking 10 .png files (each about 5 mb) was on average 5.7 secs. using header check for the same thing took average of 7.8 seconds!
使用curl检查10个.png文件(每个约5 mb)平均为5.7秒。使用标题检查同样的事情平均需要7.8秒!
So in our test curl was much faster if you have to check larger files!
所以在我们的测试中,如果你需要检查更大的文件,卷曲会更快!
our curl function is:
我们的卷曲功能是:
function remote_file_exists($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( $httpCode == 200 ){return true;}
return false;
}
here is our header check sample:
这是我们的标题检查示例:
function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}
#5
1
Do a request with curl and see if it returns a 404 status code. Do the request using the HEAD request method so it only returns the headers without a body.
使用curl执行请求并查看它是否返回404状态代码。使用HEAD请求方法执行请求,以便它只返回没有正文的标题。
#6
0
There is another Skywalker
For anyone searching and not wanting to make requests to the file, there is a way! Simply use $_SERVER['DOCUMENT_ROOT']
and the relative path from the url (using parse_url
) and then check with file_exists
.
对于任何搜索并且不想对文件发出请求的人,都有办法!只需使用$ _SERVER ['DOCUMENT_ROOT']和url的相对路径(使用parse_url),然后使用file_exists进行检查。
$_SERVER['DOCUMENT_ROOT']
does not guarantee having a/no trailing slash, though, so I have accounted for this in my example below.
但是,$ _SERVER ['DOCUMENT_ROOT']不保证有/不带尾随斜杠,所以我在下面的例子中说明了这一点。
Useful links
$_SERVER indices (see 'DOCUMENT_ROOT')
parse_url
file_exists
$ _SERVER索引(参见'DOCUMENT_ROOT')parse_url file_exists
Example
// Your URL here!
$my_url = 'https://*.com/opensearch.xml';
// Get document root
$doc_root = $_SERVER['DOCUMENT_ROOT'];
// Account for possible trailing slash
if( substr( $doc_root, strlen( $doc_root )-1, 1 ) == '/' ){
$doc_root = substr( $doc_root, 0, strlen( $doc_root - 1 ) );
}
// File location
$file = $doc_root . parse_url( $my_url )['path'];
// Update the src in the global if the child file exists
if( file_exists( $file ) ){
// You've got yourself a file
}else{
// File? What file? There's no file here...
}