dongmale0656 2008-10-30 23:51 采纳率: 100%
浏览 17
已采纳

URL的等效is_file()函数?

What is the best way to check if a given url points to a valid file (i.e. not return a 404/301/etc.)? I've got a script that will load certain .js files on a page, but I need a way to verify each URL it receives points to a valid file.

I'm still poking around the PHP manual to see which file functions (if any) will actually work with remote URLs. I'll edit my post as I find more details, but if anyone has already been down this path feel free to chime in.

  • 写回答

3条回答 默认 最新

  • dongyan4424 2008-10-31 02:01
    关注

    The file_get_contents is a bit overshooting the purpose as it is enough to have the HTTP header to make the decision, so you'll need to use curl to do so:

    <?php
    // create a new cURL resource
    $ch = curl_init();
    
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    
    // grab URL and pass it to the browser
    curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VB.NET画图时的撤销编程
  • ¥15 C# webservice 参数特殊字符处理
  • ¥15 关于控制时间更新的一个操作
  • ¥15 JAVA中使用poi创建word文件,实现页面边框环绕页眉
  • ¥15 matlibplot绘图图形切线问题
  • ¥15 flink 深度优先搜索
  • ¥15 python for everybody 使用Beautifulsoup构建soup对象的时候疯狂报错
  • ¥15 mac playcover上原神在登录后的加载页面闪退,重新下载原神IPA和playcover均无效
  • ¥20 node-dev能不能离线安装?
  • ¥20 六子棋博弈基于Alpha-Beta剪枝搜索算法的优化