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

不言
Release: 2023-04-03 15:34:01
Original
2215 people have browsed it

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;
	}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!