代码之家  ›  专栏  ›  技术社区  ›  undone

使用PHP在服务器之间传输文件

php
  •  3
  • undone  · 技术社区  · 15 年前
    Hello
    I wrote a little code in php that enables me to download a file from one
     website to my own!
    but there is a little problem here!
    I can only Download files from my website that are less than 4MB:(
    Now my question is that in what way I can Download special 
    part of files from other websites!
    like from 1st byte to 10th byte!
    
    And second question is how to get file 
    Information before starting Download!
    (I want files size for downloading)
    

    http://www.polarpengi.gigfa.com/code.htm

    2 回复  |  直到 15 年前
        1
  •  4
  •   barbushin    15 年前
    function downloadFileFromUrl($url, $dstFilepath) {
        $fr = @fopen($url, 'r');
        if($fr === false) {
            throw new Primage_Proxy_Storage_SourceNotFound($url);
        }
        $fw = fopen($dstFilepath, 'w');
        if($fw === false) {
            throw new Exception('Writing to file "' . $dstFilepath . '" failed');
        }
    
        $timeLimit = 1000;
        set_time_limit($timeLimit);
        $deadline = time() + 1000;
    
        while(!feof($fr)) {
            $bufferString = fread($fr, 10000);
            fwrite($fw, $bufferString);
            if($deadline - time() < 10) {
                fclose($fw);
                fclose($fr);
                unlink($dstFilepath);
                throw new Primage_Proxy_Storage_SourceNotFound($url);
            }
        }
        fclose($fw);
        fclose($fr);
    }
    
        2
  •  0
  •   lock    15 年前

    可以定义curl操作将读取的字节数限制,方法是:

    curl_setopt($ch, CURLOPT_RANGE,"1-2000"); //where 1-2000 range of downloaded bytes 
    

    不过,我认为使用curl下载文件并不合适。 使用 file 相反。

    推荐文章