下载带有进度的文件file_put_contents [英] Download files file_put_contents with progress

查看:107
本文介绍了下载带有进度的文件file_put_contents的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用下载文件编写代码并返回状态(下载的字节). 要下载文件,我使用file_put_contents,它可以正常工作.

I try to write code with download file and return status (downloaded bytes). To download file I use file_put_contents and it's work.

function downloadLink($link,$destination)
{
    $ctx = stream_context_create();
    stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
    $mb_download = file_put_contents($destination, fopen($link, 'r'),null,$ctx);
    return $mb_download;
}

function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
    file_put_contents( 'progress.txt', '' );
    $fp = fopen('progress.txt', 'a' );
    fputs( $fp,$bytes_transferred);
    fclose( $fp );
    echo 1;
}

这是我的职责.我有使用回调函数的问题,因为所有函数都在同一个类中.现在不使用stream_notification_callback.我尝试将声明更改为

It's my functions. I have problem to use callback function because all function is inside the same class. Now stream_notification_callback is not use. I try change declaration to

stream_context_set_params($ctx, array("notification" => "$this->stream_notification_callback()"));

stream_context_set_params($ctx, array("notification" => $this->stream_notification_callback()));

但是它不起作用.

推荐答案

您应该尝试

stream_context_set_params($ctx, array(
    "notification" => array($this, 'stream_notification_callback')
));

这篇关于下载带有进度的文件file_put_contents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆