duangou6446 2011-10-31 11:18
浏览 45
已采纳

PHP,检查URL和文件是否存在?

I create a plugin for WordPress that requires two files to be exists in order to operate normaly.

The first file is defined as a file system path and the second file is defined as a URL.

Let's say the first file is that:

/home/my_site/public_html/some_folder/required_file.php

and the second file is that:

http://www.my_site.com/some_folder/required_url_file.php

Note that both files are not the same file into the file system. The required_file.php has other content than the required_url_file.php and they act absolutly diferent

Any idea on how to validate the existance of both files ?

  • 写回答

10条回答 默认 最新

  • duanli6834 2011-10-31 11:31
    关注

    You can check both:

    $file = '/home/my_site/public_html/some_folder/required_file.php';
    $url = 'http://www.my_site.com/some_folder/required_url_file.php';
    
    $fileExists = is_file($file);
    $urlExists = is_200($url);
    
    $bothExists = $fileExists && $urlExists;
    
    function is_200($url)
    {
        $options['http'] = array(
            'method' => "HEAD",
            'ignore_errors' => 1,
            'max_redirects' => 0
        );
        $body = file_get_contents($url, NULL, stream_context_create($options));
        sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
        return $code === 200;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波