I need to see if a specific image exists on my cdn.
我需要查看cdn上是否存在一个特定的映像。
I've tried the following and it doesn't work:
我试过以下方法,但没有效果:
if (file_exists(http://www.example.com/images/$filename)) {
echo "The file exists";
} else {
echo "The file does not exist";
}
Even if the image exists or doesn't exist, it always says "The file exists". I'm not sure why its not working...
即使图像存在或不存在,它总是说“文件存在”。我不知道为什么它不起作用……
17 个解决方案
#1
102
You need the filename in quotation marks at least (as string):
您至少需要以引号括起文件名(作为字符串):
if (file_exists('http://www.mydomain.com/images/'.$filename)) {
… }
Also, make sure $filename
is properly validated. And then, it will only work when allow_url_fopen
is activated in your PHP config
另外,确保$filename已被正确验证。然后,只有当在PHP配置中激活allow_url_fopen时,它才会工作
#2
92
if (file_exists('http://www.mydomain.com/images/'.$filename)) {}
This didn't work for me. The way I did it was using getimagesize.
这对我不起作用。我使用getimagesize。
$src = 'http://www.mydomain.com/images/'.$filename;
if (@getimagesize($src)) {
Note that the '@' will mean that if the image does not exist (in which case the function would usually throw an error: getimagesize(http://www.mydomain.com/images/filename.png) [function.getimagesize]: failed
) it will return false.
注意,“@”将意味着如果图像不存在(在这种情况下,函数通常会抛出一个错误:getimagesize(http://www.mydomain.com/images/filename.png) [function]。失败)它将返回false。
#3
12
Try like this:
试试这样:
$file = '/path/to/foo.txt'; // 'images/'.$file (physical path)
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
#4
12
Well, file_exists
does something weird, it does not say if a file exists, it says if path exists.
file_exists会做一些奇怪的事情,它不会说某个文件是否存在,而是说路径是否存在。
So, to check if it is a file then you should use is_file
together with file_exists
to know if there is really a file behind the path, otherwise file_exists
will return true
for any existing path.
因此,要检查它是否是一个文件,那么您应该使用is_file和file_exists来知道路径后面是否真的有一个文件,否则file_exists将返回任何现有路径的true。
Here is the function i use :
这里是我使用的函数:
function fileExists($filePath)
{
return is_file($filePath) && file_exists($filePath);
}
#5
8
A thing you have to understand first: you have no files.
A file is a subject of a filesystem, but you are making your request using HTTP protocol which supports no files but URLs.
首先要明白的一点是:你没有文件。文件是文件系统的主题,但是您使用的是HTTP协议,它只支持url,不支持任何文件。
So, you have to request an unexisting file using your browser and see the response code. if it's not 404, you are unable to use any wrappers to see if a file exists and you have to request your cdn using some other protocol, FTP for example
因此,您必须使用浏览器请求一个未存在的文件,并查看响应代码。如果不是404,就不能使用任何包装器来查看文件是否存在,必须使用其他协议(例如FTP)请求cdn
#6
7
If the file is on your local domain, you don't need to put the full URL. Only the path to the file. If the file is in a different directory, then you need to preface the path with "."
如果该文件位于您的本地域,则不需要放置完整的URL。只有文件的路径。如果文件位于不同的目录中,则需要在路径前面加上“”。
$file = './images/image.jpg';
if (file_exists($file)) {}
Often times the "." is left off which will cause the file to be shown as not existing, when it in fact does.
通常情况下,“.”被省略,这将导致文件显示为不存在,而实际上它是存在的。
#7
7
Here is the simplest way to check if a file exist:
这是检查文件是否存在的最简单方法:
if(is_file($filename)){
return true; //the file exist
}else{
return false; //the file does not exist
}
#8
5
public static function is_file_url_exists($url) {
if (@file_get_contents($url, 0, NULL, 0, 1)) {
return 1;
}
return 0;
}
#9
3
There is a major difference between is_file
and file_exists
.
is_file和file_exist之间有一个很大的区别。
is_file
returns true for (regular) files:
is_file(常规)文件返回true:
Returns TRUE if the filename exists and is a regular file, FALSE otherwise.
如果文件名存在且是常规文件,则返回TRUE,否则返回FALSE。
file_exists
returns true for both files and directories:
file_exists对文件和目录返回true:
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
如果文件名指定的文件或目录存在,则返回TRUE;否则错误。
Note: Check also this * question for more information on this topic.
注意:还要检查这个*问题,以获得关于这个主题的更多信息。
#10
2
You have to use absolute path to see if the file exists.
您必须使用绝对路径来查看文件是否存在。
$abs_path = '/var/www/example.com/public_html/images/';
$file_url = 'http://www.example.com/images/' . $filename;
if (file_exists($abs_path . $filename)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
If you are writing for CMS or PHP framework then as far as I know all of them have defined constant for document root path.
如果您是为CMS或PHP框架编写,那么就我所知,它们都为文档根路径定义了常量。
e.g WordPress uses ABSPATH which can be used globally for working with files on the server using your code as well as site url.
e。WordPress使用ABSPATH,可以在服务器上使用您的代码和站点url处理文件。
Wordpress example:
Wordpress的例子:
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
if (file_exists($image_path)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
I'm going an extra mile here :). Because this code would no need much maintenance and pretty solid, I would write it with as shorthand if statement:
我在这里多走了一英里。因为这段代码不需要太多维护和非常坚固,所以我将它作为一个简写来写:
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
echo (file_exists($image_path))?'The file exists. URL:' . $file_url:'The file does not exist';
Shorthand IF statement explained:
速记IF语句解释道:
$stringVariable = ($trueOrFalseComa* > 0)?'String if true':'String if false';
#11
2
you can use cURL. You can get cURL to only give you the headers, and not the body, which might make it faster. A bad domain could always take a while because you will be waiting for the request to time-out; you could probably change the timeout length using cURL.
您可以使用cURL。你可以使旋度只给你头部,而不是身体,这可能使它更快。一个糟糕的域总是需要一段时间,因为您将等待请求超时;您可以使用cURL来更改超时长度。
Here is example:
下面是例子:
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;
}
$exists = remoteFileExists('http://*.com/favicon.ico');
if ($exists) {
echo 'file exists';
} else {
echo 'file does not exist';
}
#12
1
You can use the file_get_contents
function to access remote files. See http://php.net/manual/en/function.file-get-contents.php for details.
可以使用file_get_contents函数访问远程文件。有关详细信息,请参阅http://php.net/manual/en/function.file-get-contents.php。
#13
1
try this :
试试这个:
if (file_exists(FCPATH . 'uploads/pages/' . $image)) {
unlink(FCPATH . 'uploads/pages/' . $image);
}
#14
0
Read first 5 bytes form HTTP using fopen()
and fread()
then use this:
使用fopen()和fread()从HTTP中读取前5个字节,然后使用以下命令:
DEFINE("GIF_START","GIF");
DEFINE("PNG_START",pack("C",0x89)."PNG");
DEFINE("JPG_START",pack("CCCCCC",0xFF,0xD8,0xFF,0xE0,0x00,0x10));
to detect image.
检测图像。
#15
0
file_exists
reads not only files, but also paths. so when $filename
is empty, the command would run as if it's written like this:
file_exists不仅读取文件,还读取路径。因此,当$filename为空时,命令将运行,就像它是这样写的:
file_exists("http://www.example.com/images/")
if the directory /images/ exists, the function will still return true
.
如果目录/图像/存在,函数仍然返回true。
I usually write it like this:
我通常这样写:
// !empty($filename) is to prevent an error when the variable is not defined
if (!empty($filename) && file_exists("http://www.example.com/images/$filename"))
{
// do something
}
else
{
// do other things
}
#16
0
file_exists($filepath)
will return a true result for a directory and full filepath, so is not always a solution when a filename is not passed.
file_exists($filepath)将返回一个目录和完整文件路径的真实结果,所以当文件名没有通过时,不总是一个解决方案。
is_file($filepath)
will only return true for fully filepaths
is_file($filepath)只对完整的文件路径返回true
#17
0
If you are using curl, you can try the following script:
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
}
Reference URL: https://hungred.com/how-to/php-check-remote-email-url-image-link-exist/
参考网址:https://hungred.com/how-to/php-check-remote-email-url-image-link-exist/
#1
102
You need the filename in quotation marks at least (as string):
您至少需要以引号括起文件名(作为字符串):
if (file_exists('http://www.mydomain.com/images/'.$filename)) {
… }
Also, make sure $filename
is properly validated. And then, it will only work when allow_url_fopen
is activated in your PHP config
另外,确保$filename已被正确验证。然后,只有当在PHP配置中激活allow_url_fopen时,它才会工作
#2
92
if (file_exists('http://www.mydomain.com/images/'.$filename)) {}
This didn't work for me. The way I did it was using getimagesize.
这对我不起作用。我使用getimagesize。
$src = 'http://www.mydomain.com/images/'.$filename;
if (@getimagesize($src)) {
Note that the '@' will mean that if the image does not exist (in which case the function would usually throw an error: getimagesize(http://www.mydomain.com/images/filename.png) [function.getimagesize]: failed
) it will return false.
注意,“@”将意味着如果图像不存在(在这种情况下,函数通常会抛出一个错误:getimagesize(http://www.mydomain.com/images/filename.png) [function]。失败)它将返回false。
#3
12
Try like this:
试试这样:
$file = '/path/to/foo.txt'; // 'images/'.$file (physical path)
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
#4
12
Well, file_exists
does something weird, it does not say if a file exists, it says if path exists.
file_exists会做一些奇怪的事情,它不会说某个文件是否存在,而是说路径是否存在。
So, to check if it is a file then you should use is_file
together with file_exists
to know if there is really a file behind the path, otherwise file_exists
will return true
for any existing path.
因此,要检查它是否是一个文件,那么您应该使用is_file和file_exists来知道路径后面是否真的有一个文件,否则file_exists将返回任何现有路径的true。
Here is the function i use :
这里是我使用的函数:
function fileExists($filePath)
{
return is_file($filePath) && file_exists($filePath);
}
#5
8
A thing you have to understand first: you have no files.
A file is a subject of a filesystem, but you are making your request using HTTP protocol which supports no files but URLs.
首先要明白的一点是:你没有文件。文件是文件系统的主题,但是您使用的是HTTP协议,它只支持url,不支持任何文件。
So, you have to request an unexisting file using your browser and see the response code. if it's not 404, you are unable to use any wrappers to see if a file exists and you have to request your cdn using some other protocol, FTP for example
因此,您必须使用浏览器请求一个未存在的文件,并查看响应代码。如果不是404,就不能使用任何包装器来查看文件是否存在,必须使用其他协议(例如FTP)请求cdn
#6
7
If the file is on your local domain, you don't need to put the full URL. Only the path to the file. If the file is in a different directory, then you need to preface the path with "."
如果该文件位于您的本地域,则不需要放置完整的URL。只有文件的路径。如果文件位于不同的目录中,则需要在路径前面加上“”。
$file = './images/image.jpg';
if (file_exists($file)) {}
Often times the "." is left off which will cause the file to be shown as not existing, when it in fact does.
通常情况下,“.”被省略,这将导致文件显示为不存在,而实际上它是存在的。
#7
7
Here is the simplest way to check if a file exist:
这是检查文件是否存在的最简单方法:
if(is_file($filename)){
return true; //the file exist
}else{
return false; //the file does not exist
}
#8
5
public static function is_file_url_exists($url) {
if (@file_get_contents($url, 0, NULL, 0, 1)) {
return 1;
}
return 0;
}
#9
3
There is a major difference between is_file
and file_exists
.
is_file和file_exist之间有一个很大的区别。
is_file
returns true for (regular) files:
is_file(常规)文件返回true:
Returns TRUE if the filename exists and is a regular file, FALSE otherwise.
如果文件名存在且是常规文件,则返回TRUE,否则返回FALSE。
file_exists
returns true for both files and directories:
file_exists对文件和目录返回true:
Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
如果文件名指定的文件或目录存在,则返回TRUE;否则错误。
Note: Check also this * question for more information on this topic.
注意:还要检查这个*问题,以获得关于这个主题的更多信息。
#10
2
You have to use absolute path to see if the file exists.
您必须使用绝对路径来查看文件是否存在。
$abs_path = '/var/www/example.com/public_html/images/';
$file_url = 'http://www.example.com/images/' . $filename;
if (file_exists($abs_path . $filename)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
If you are writing for CMS or PHP framework then as far as I know all of them have defined constant for document root path.
如果您是为CMS或PHP框架编写,那么就我所知,它们都为文档根路径定义了常量。
e.g WordPress uses ABSPATH which can be used globally for working with files on the server using your code as well as site url.
e。WordPress使用ABSPATH,可以在服务器上使用您的代码和站点url处理文件。
Wordpress example:
Wordpress的例子:
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
if (file_exists($image_path)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
I'm going an extra mile here :). Because this code would no need much maintenance and pretty solid, I would write it with as shorthand if statement:
我在这里多走了一英里。因为这段代码不需要太多维护和非常坚固,所以我将它作为一个简写来写:
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
echo (file_exists($image_path))?'The file exists. URL:' . $file_url:'The file does not exist';
Shorthand IF statement explained:
速记IF语句解释道:
$stringVariable = ($trueOrFalseComa* > 0)?'String if true':'String if false';
#11
2
you can use cURL. You can get cURL to only give you the headers, and not the body, which might make it faster. A bad domain could always take a while because you will be waiting for the request to time-out; you could probably change the timeout length using cURL.
您可以使用cURL。你可以使旋度只给你头部,而不是身体,这可能使它更快。一个糟糕的域总是需要一段时间,因为您将等待请求超时;您可以使用cURL来更改超时长度。
Here is example:
下面是例子:
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;
}
$exists = remoteFileExists('http://*.com/favicon.ico');
if ($exists) {
echo 'file exists';
} else {
echo 'file does not exist';
}
#12
1
You can use the file_get_contents
function to access remote files. See http://php.net/manual/en/function.file-get-contents.php for details.
可以使用file_get_contents函数访问远程文件。有关详细信息,请参阅http://php.net/manual/en/function.file-get-contents.php。
#13
1
try this :
试试这个:
if (file_exists(FCPATH . 'uploads/pages/' . $image)) {
unlink(FCPATH . 'uploads/pages/' . $image);
}
#14
0
Read first 5 bytes form HTTP using fopen()
and fread()
then use this:
使用fopen()和fread()从HTTP中读取前5个字节,然后使用以下命令:
DEFINE("GIF_START","GIF");
DEFINE("PNG_START",pack("C",0x89)."PNG");
DEFINE("JPG_START",pack("CCCCCC",0xFF,0xD8,0xFF,0xE0,0x00,0x10));
to detect image.
检测图像。
#15
0
file_exists
reads not only files, but also paths. so when $filename
is empty, the command would run as if it's written like this:
file_exists不仅读取文件,还读取路径。因此,当$filename为空时,命令将运行,就像它是这样写的:
file_exists("http://www.example.com/images/")
if the directory /images/ exists, the function will still return true
.
如果目录/图像/存在,函数仍然返回true。
I usually write it like this:
我通常这样写:
// !empty($filename) is to prevent an error when the variable is not defined
if (!empty($filename) && file_exists("http://www.example.com/images/$filename"))
{
// do something
}
else
{
// do other things
}
#16
0
file_exists($filepath)
will return a true result for a directory and full filepath, so is not always a solution when a filename is not passed.
file_exists($filepath)将返回一个目录和完整文件路径的真实结果,所以当文件名没有通过时,不总是一个解决方案。
is_file($filepath)
will only return true for fully filepaths
is_file($filepath)只对完整的文件路径返回true
#17
0
If you are using curl, you can try the following script:
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
}
Reference URL: https://hungred.com/how-to/php-check-remote-email-url-image-link-exist/
参考网址:https://hungred.com/how-to/php-check-remote-email-url-image-link-exist/