check if file exists in php

前端 未结 6 1683
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 17:02
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {   
$filefound = \'0\';                         
}

why won\'t this work?

6条回答
  •  攒了一身酷
    2020-12-03 17:22

    You can also use PHP get_headers() function.

    Example:

    function check_file_exists_here($url){
       $result=get_headers($url);
       return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
    }
    
    if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
       echo "This file exists";
    else
       echo "This file does not exist";
    

提交回复
热议问题