我有mysql表 hravaj00_dily ,列 part_id , img150 , imgfull 。在img150和imgfull中存储了图像的URL。 此表格从xml feed btw。
更新是否有任何PHP解决方案要通过列img150(或imgfull),检查url是否存在(404错误)并使用不存在的URL从数据库中删除所有这些行..?
我已经阅读过下面这个函数,它检查了url的http头。这有点用吗?我不知道如何使用它。
function file_external_exists($url)
{
$headers = @get_headers($url);
if(preg_match("|200|",$headers[0]))
return(true);
else return(false);
}
答案 0 :(得分:2)
$con=mysqli_connect("example.com","peter","abc123","my_db");
$result = mysqli_query($con,"SELECT * FROM hravaj00_dily");
while($row = mysqli_fetch_array($result)) {
$url = $row['img150'];
if(!urlExists($url)) {
$nonExistent[] = $row['id']; // Assuming you have primary key
}
}
if($nonExistent) {
$nonExistentCSV = implode(",", $nonExistent);
$delQuery = "DELETE FROM hravaj00_dily WHERE id IN " . $nonExistentCSV;
mysqli_query($con, $delQuery);
}
mysqli_close($con);
// Ref: http://stackoverflow.com/questions/408405/easy-way-to-test-a-url-for-404-in-php
function urlExists($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 200) {
curl_close($handle);
return true;
}
curl_close($handle);
return false;
}
答案 1 :(得分:0)