简体   繁体   中英

PHP. Download from FTP with stream or custom ftp wrapper

I need help with creation of custom FTP wrapper in PHP.

I used

$h=fopen('ftp://....');

But this doesn't work well with all FTP servers (sometimes problems with active/passive mode) and i can not control this with the wrapper.

I use functions from http://php.net/manual/en/book.ftp.php and all works fine. I have problems only with download with PHP FTP wrapper.

I need to use wrapper because i don't want to store downloaded file to the disk. I read part of a file from a stream and then output to a user browser.

So i want to create custom FTP wrapper in PHP. And best of all if it is based on functions from there http://php.net/manual/en/book.ftp.php .

I see example there http://php.net/manual/en/function.ftp-fget.php

function ftp_get_contents ($conn_id, $filename, 
//Create temp handler: 
     $tempHandle = fopen('php://temp', 'r+'); 

//Get file from FTP assuming that it exists: 
     ftp_fget($conn_id, $tempHandle, $filename, FTP_ASCII, 0)); 

     //Getting detailed stats to check filesize: 
     $fstats = fstat($tempHandle); 

     return fread($tempHandle, $fstats['size']); 
 }

But this is not good as it saves full file to the disk and only after this i can read from the local file.

DO you know something like this but allows me to read file with portions from FTP server without caching full file locally?

Thanks.

I found a solution.

i do

$h=popen('wget -q -O - ftp://.....','r');

it works fine. But wget must be installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM