| //php如何判断文件是否存在,包括本地和远程文件 function my_file_exists($file) { if(preg_match('/^http:\/\//',$file)){ //远程文件 if(ini_get('allow_url_fopen')){ if(@fopen($file,'r')) return true; } else{ $parseurl=parse_url($file); $host=$parseurl['host']; $path=$parseurl['path']; $fp=fsockopen($host,80, $errno, $errstr, 10); if(!$fp)return false; fputs($fp,"GET {$path} HTTP/1.1 \r\nhost:{$host}\r\n\r\n"); if(preg_match('/HTTP\/1.1 200/',fgets($fp,1024))) return true; } return false; } return file_exists($file); } |
| //$imagepath = $uploadsImg.'images/'.$savename; $imagepath ='../uploadfiles/files/'.$savename; if(file_exists($imagepath)){ unlink($imagepath); } |

本文介绍了一种PHP中检查文件是否存在的方法,包括本地文件和远程文件。对于远程文件,提供了两种不同的检查方式:一种利用fopen直接访问,另一种通过fsockopen发起HTTP请求来验证。
553

被折叠的 条评论
为什么被折叠?



