我有一个网站http://www.reelfilmlocations.co.uk
上面的网站有一个管理区域,在那里上传图像,并在上传/图像目录的子文件夹中创建不同大小的副本。
我正在为移动设备创建一个网站,它将在子域上运行,但使用来自主域的数据库和图像,http://2012.reelfilmlocations.co.uk
我希望能够访问父域上的图像,这可以通过链接到完整域的图像来实现,即http://www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg
虽然我需要先检查图像是否存在。。。
我有一个php函数,它检查图像是否存在,如果存在,它将返回图像的完整url。
如果它不存在,我想返回占位符图像的路径。
我有以下函数,如果存在,则返回正确的图像,但如果不存在,则仅返回占位符图像所在目录的路径,即http://www.reelfilmlocations.co.uk/images/thumbs/
。没有no-image.jpg位。
有问题的页面是:http://2012.reelfilmlocations.co.uk/browse-unitbases/我在页面上获取图像的代码是:
<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);
}
}
未能做到这一点,我四处挖掘,阅读了一些关于卷曲的帖子:
这只是每次返回一个占位符图像。
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);
}
}
我不知道这是否与获得403分有关,也不知道如何检查是否是这样。
我能尝试的任何建议或事情都将不胜感激。
我会使用CURL,发出HEAD请求并检查响应代码。
没有测试,但应该做到:
$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://
前缀,因为我是在命令行中这样做的。
当然,使用PHP的CURL函数也可以获得相同的结果,并且上面的调用可能在您的服务器上不起作用。我只是在解释,如果我有同样的需求,我会做什么。