Home  >  Article  >  Backend Development  >  How to check whether a remote file exists in php (pure code)

How to check whether a remote file exists in php (pure code)

不言
不言Original
2018-08-04 11:35:412164browse

The content of this article is about how to check whether remote files exist in PHP (pure code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

//检查远程文件是否存在
	function check_remote_file_exists($url)
	{
		$curl = curl_init($url);
		// 不取回数据
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); //不加这个会返回403,加了才返回正确的200,原因不明
		// 发送请求
		$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;
	}

Recommended related articles:

Code examples of how PHP deletes a directory and all files in the directory

How PHP gets the IP it belongs to Code example for region (pure code)

The above is the detailed content of How to check whether a remote file exists in php (pure code). For more information, please follow other related articles on the PHP Chinese website!

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