2012-11-16 94 views

回答

0

至于优素福梅蒙评论(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个字节)。

但是,我不能充满信心地判断这是否会始终有效,因为我认为这也取决于块的大小。

相关问题