簡體   English   中英

如何判斷整個文件是否已使用stream_copy_to_stream下載?

[英]How to tell if the whole file has been downloaded using stream_copy_to_stream?

http://php.net/stream_copy_to_stream $maxlength參數允許限制要復制的字節數。

如何判斷是否違反了限制,即 不是下載整個文件?

關於Yousuf Memon評論(http://mattgemmell.com/2008/12/08/what-have-you-tried/),我的解決方法很簡單:

public function download($size_limit)
{
    if($this->temp_file)
    {
        throw new UploadRemoteImageException('Resource has been downloaded already.');
    }

    $this->temp_file    = tempnam(sys_get_temp_dir(), $this->temp_file_prefix);

    $src    = fopen($this->url, 'r');
    $dest   = fopen($this->temp_file, 'w+');

    stream_copy_to_stream($src, $dest, $size_limit+1000);

    if(filesize($dest) > $size_limit)
    {
        // The file size limit has been breached.
    }

    // [..]
}

通過簡單地在用戶定義的限制之上添加更多字節來工作。 然后,在關閉流時,它檢查文件是否大於用戶定義的大小限制(這可能是因為我們在頂部增加了1000個字節)。

但是,我不確定這是否總是可行,因為我認為這也取決於塊的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM