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

推荐答案

Here:

$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

From here and right below the above post, there's a curl solution:

function url_exists($url) {
    return curl_init($url) !== false;
}

Php相关问答推荐

从含有特殊字符的ANSI文本文件中读取数据是在移值,为什么?

在Laravel Eloquent中实现一对多关系

不包括WooCommerce中单独销售的产品

如何在WooCommerce产品子类别中显示特定内容

如何在laravel中使用script type= text/html/script

在WooCommerce获取所有具有全球产品属性品牌的简单产品

在WooCommerce上克隆订单时使用`add_product()`添加产品元数据

如何使用barryvdh/laravel-Snappy在两个打印页面上将表格分成两部分?

将可编辑的计费自定义字段添加到 WooCommerce 管理单个订单

laravel Eloquent 模型的问题

奇怪的 preg_match_all() 行为

使用 phpseclib 验证 RSA-PSS

PHP 创建 CSV 文件并用波斯语写入

使用现有的htaccess的PHP路由脚本

PHP使用三元运算符进行null合并(再次)

symfony/notifier + twig/inky-extra 在 dunglas/symfony-docker 中缺少 ext-xsl.

在 WordPress 中使用 $wpdb 运行 MySQL 事务是否安全?

同时执行odbc_execute、odbc_fetch_row和odbc_result

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

通过php在xml文件中添加 node