Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试PHP`copy`是否可以从internet下载文件_Php_Laravel_Bitbucket Pipelines - Fatal编程技术网

测试PHP`copy`是否可以从internet下载文件

测试PHP`copy`是否可以从internet下载文件,php,laravel,bitbucket-pipelines,Php,Laravel,Bitbucket Pipelines,我编写了一个函数,通过单元测试,使用PHP下载一个图像 // ... if (!copy($url, $imagePath)) { return null; } // ... 它在本地工作,但在单元测试中失败。无法下载文件(在存储器中找不到文件) 它可能是故意禁用的。因此,我只想在copy()可以下载外部文件的情况下运行此单元测试 我试过了,但没有成功: public function test_downloadImage() { if (!ini_get('allow_url

我编写了一个函数,通过单元测试,使用PHP下载一个图像

// ...
if (!copy($url, $imagePath)) {
    return null;
}
// ...
它在本地工作,但在单元测试中失败。无法下载文件(在存储器中找不到文件)

它可能是故意禁用的。因此,我只想在
copy()
可以下载外部文件的情况下运行此单元测试

我试过了,但没有成功:

public function test_downloadImage()
{
    if (!ini_get('allow_url_fopen') || in_array('copy', explode(',', ini_get('disable_functions')))) {
        return;
    }
    // download the image...
    // assert file exists...
}
如何测试
copy()
是否可以下载外部文件

多谢各位

问题解决 对此很抱歉,但问题并非来自PHP
copy()


它试图将一个映像下载到一个不存在的目录中。事实上,我忘了设置。它已经在我的计算机上设置好了。

考虑切换到curl,因为
允许url\u fopen
通常出于安全原因被禁用


但是,要回答您的问题,您可以使用为file_get_contents()描述的方法来检查是否可以从web获取内容

  • 文件\u get\u contents
    前面用@抑制警告(如建议的,但如果可以避免,请不要使用此选项)
  • 检查
    null的返回值
  • 抛出异常
  • 例如:

    public function test_downloadImage($path)
    {
        $contents = @file_get_contents($path);
        if($contents === null) {
            $error = error_get_last();
            throw new Exception($error['message']);
        }
        return $contents;
    }
    
    使用try/catch调用此函数:

    try {
        $fileContent = test_downloadImage('http://stackoverflow.com')
        // Success, do something with your file
    } catch (Exception $e) {
        // Download failed, log error from $e->getMessage(), show message to user
    }
    

    使用
    curl
    启动HEAD请求:

    function url_is_readable($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        $res = curl_exec($ch);
        return false !== $res;
    }
    
    然后进行测试:

    var_dump(url_is_readable('https://imgs.xkcd.com/comics/im_so_random.png'));
    var_dump(url_is_readable('http://whyohwhy.example.co/monkey.png'));
    

    然后可以使用
    curl
    执行复制。这有一些好处。首先,它的工作与是否关闭了
    allow\u url\u fopen
    无关。其次,
    curl
    为您提供了更多的控制、诊断和错误信息。第三,天气凉爽。:)

    你可以用卷曲来代替吗?它是一个功能更加全面的远程文件获取客户端。它有超时设置,它返回HTTP响应代码,等等。您可以轻松地检查它是否安装了扩展名加载(“curl”),只需将文件内容与URL一起使用即可。请记住,您不是在为客户端构建,除非您正在处理一个可能分布在各地的开放源码项目,否则一旦它在您的服务器上运行,您就已经准备好了。请不要建议
    @
    -suppression.True,但是除了
    set\u error\u handler
    之外,似乎没有其他解决这个问题的方法,它可能在启用
    ini\u get
    时被禁用。我将编辑我的答案。PHP7允许您在
    try
    -
    catch
    块中捕获这些异常。然后您必须首先检查是否正在运行PHP7。。。事实上,PHP手册建议在这种情况下使用@:谢谢您的回答,但问题实际上来自其他地方。请看我的编辑。