 |
|
คือผมทำเว็บ download mp3 ครับ เก็บ file ไว้อีก host นึง
เลยจำเป็นต้อง check file ก่อนว่ามีอยู่ไหม ถึงจะ show ปุ่ม download
Code (PHP)
<?php
function url_exists($url) {
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
$head = substr($head,0,strpos($head, 'Connection: close'));
fclose($fid);
if (preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)) {
$pos = strpos($head, 'Content-Type');
return $pos !== false;
}
} else {
return false;
}
}
$x = url_exists("http://www.websitethailand.biz/images/flag_thai.jpg");
if($x == true){
echo "yes";
}else{
echo "no";
}
?>
|
 |
 |
 |
 |
Date :
2010-03-17 01:28:30 |
By :
peterxp |
|
 |
 |
 |
 |
|
|
 |