How can I check if a URL exists via PHP?

前端 未结 22 1746
天涯浪人
天涯浪人 2020-11-22 04:13

How do I check if a URL exists (not 404) in PHP?

22条回答
  •  庸人自扰
    2020-11-22 04:56

    Here is a solution that reads only the first byte of source code... returning false if the file_get_contents fails... This will also work for remote files like images.

     function urlExists($url)
    {
        if (@file_get_contents($url,false,NULL,0,1))
        {
            return true;
        }
        return false;
    }
    

提交回复
热议问题