Home  >  Article  >  Backend Development  >  PHP determines whether remote files are available in advance_PHP tutorial

PHP determines whether remote files are available in advance_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:12933browse

We used php curl related functions to access remote files, and then judged whether the file can be used normally based on the return status. Friends in need can refer to

The code is as follows
 代码如下 复制代码

//判断远程文件
function check_remote_file_exists($url)
{
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
// 发送请求
$result = curl_exec($curl);
$found = false;
// 如果请求没有发送失败
if ($result !== false) {
// 再检查http响应码是否为200
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$found = true;
}
}
curl_close($curl);

return $found;
}

Copy code

//Judge remote files
function check_remote_file_exists($url)
{
$curl = curl_init($url);
//Do not retrieve data
curl_setopt($curl, CURLOPT_NOBODY, true);
//Send request
$result = curl_exec($curl);
$found = false;
//Failed if the request was not sent
if ($result !== false) {
// Check again whether the http response code is 200
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$found = true;
}
}
curl_close($curl);

 代码如下 复制代码
$url = "/upload/201110/20111008192257383.gif";
$array = get_headers($url,1);
if(preg_match('/200/',$array[0])){
echo "
"; 
print_r($array);
}else{
echo "无效url资源!";
}
return $found;
}

Method 2

http://www.bkjia.com/PHPjc/444739.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/444739.html
TechArticleWe use php curl related functions to access remote files, and then judge whether the file can be used normally based on the return status. Friends in need can refer to the code and copy it as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn