php检查外部doman上是否存在文件(访问子域) [英] php check if file exists on an external doman (accessing form a sub domain)
问题描述
我在 http://www.reelfilmlocations.co.uk 上有一个网站
上述网站有一个管理区域,上传图片并在uploads / images目录的子文件夹中创建不同大小的副本。
The above site has an admin area where images are uploaded and different size copies created in subfolders of an uploads/images directory.
我创建一个移动设备的网站,它将在子域操作,但使用主域中的数据库和图像
http://2012.reelfilmlocations.co.uk
I am creating a site for mobile devices, which will operate on a sub-domain, but use the database and images from the main domain, http://2012.reelfilmlocations.co.uk
我想要能够访问父域,我可以通过链接到具有完整域的图像做的http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg
I want to be able to access the images that are on the parent domain, which i can do by linking to the image with the full domain i.e http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg
虽然我需要检查图像是否存在第一...
Though i need to check if the image exists first...
我有一个php函数检查图像是否存在,如果它返回图像的完整url 。
I have a php function that checks if the images exists, if it does it returns the full url of the image.
如果它不存在,我想返回一个占位符图像的路径。
If it doesn't exist i want to return the path of a placeholder image.
函数,如果存在,返回正确的图像,但如果不存在,它只是返回到占位符图像所在的目录的路径,即 http://www.reelfilmlocations.co。 uk / images / thumbs /
。没有no-image.jpg位。
The following function i have, returns the correct image if it exists, but if it doesn't, it is just returning the path to the directory where the placeholder image resides i.e http://www.reelfilmlocations.co.uk/images/thumbs/
. without the no-image.jpg bit.
问题的页面是: http://2012.reelfilmlocations.co.uk/browse-unitbases/
我在我的页面上获得图像的代码是:
The page in question is: http://2012.reelfilmlocations.co.uk/browse-unitbases/ the code i have on my page to get the image is:
<img src="<?php checkImageExists('/uploads/images/thumbs/', $row_rs_locations['image_ubs']);?>">
我的PHP功能:
if(!function_exists("checkImageExists")){
function checkImageExists($path, $file){
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
$header_response = get_headers($imageName, 1);
if(strpos($header_response[0], "404" ) !== false ){
// NO FILE EXISTS
$imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg";
}else{
// FILE EXISTS!!
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
}
echo($imageName);
}
}
无法让这个工作,我做了一些挖掘并阅读关于curl的一些帖子:
Failing to get this to work i did some digging around and read some posts about curl:
这只是每次都返回一个占位符图片。
This just returns a placeholder image everytime.
if(!function_exists("remoteFileExists")){
function remoteFileExists($url) {
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
//do request
$result = curl_exec($curl);
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200 ) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
}
if(!function_exists("checkImageExists")){
function checkImageExists($path, $file){
$imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
$exists = remoteFileExists($imageName);
if ($exists){
// file exists do nothing we already have the correct $imageName
} else {
// file does not exist so set our image to the placeholder
$imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg";
}
echo($imageName);
}
}
我不知道是否可以a 403,或如何检查是否是这种情况。
I dont know if it could be to do with getting a 403, or how to check if this is the case.
任何指针或东西,我可以尝试woud非常感激。
any pointers or things i could try woud be greatly appreciated.
推荐答案
我会使用CURL,发出HEAD请求并检查响应代码。
I'd do it using CURL, issuing a HEAD request and checking the response code.
,但应该做的诀窍:
$URL = 'sub.domain.com/image.jpg';
$res = `curl -s -o /dev/null -IL -w "%{http_code}" http://$URL`;
if ($res == '200')
echo 'Image exists';
上面的代码将填充 $ res
申请的状态代码(注意,我不要在 $ URL
中包含 http://
The code above will populate $res
with status code of the requisition (pay attention that I DON'T include the http://
prefix to the $URL
variable because I do it in the command line.
当然,使用PHP的CURL函数也可以获得同样的效果,上面的调用可能无法在你的服务器上工作。只是说明如果我有同样的需要我会做什么。
Of course the same might be obtained using PHP's CURL functions, and the above call might not work on your server. I am just explaining what I'd be doing if I had the same need.
这篇关于php检查外部doman上是否存在文件(访问子域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!