views:

107

answers:

2

I would like to download parts of files on a FTP server. I've got this solution:

  $opts = array('ftp'=>array('overwrite'=>false, 'resume_pos'=> 5*16+12));      
  $context = stream_context_create($opts);

  $version = file_get_contents
    (
     'ftp://'.$ftpAccount["username"].':'.$ftpAccount["password"].'@'.$ftpAccount["server"].'/firm/'.$file, FILE_BINARY, $context, -1, 20
    );

I don't like this solution because it opens new connection for every file. Does anybody know a better solution (effective one)?

Thank you!

A: 

Use the FTP functions: http://us3.php.net/ftp

<?php

// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';

// open some file to write to
$handle = fopen($local_file, 'w');

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
 echo "successfully written to $local_file\n";
} else {
 echo "There was a problem while downloading $remote_file to $local_file\n";
}

// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>
Scott Saunders
Can the built-in FTP functions do partial downloads?
Pekka
This solution downloads the whole file and I need to download just part of the file.
MartyIX
The fifth parameter to ftp_fget or ftp_get is "The position in the remote file to start downloading from."
Scott Saunders
http://us3.php.net/manual/en/function.ftp-get.php
Scott Saunders
+1  A: 

Heres a similar question asked of the curl library. http://curl.haxx.se/mail/lib-2005-01/0176.html

Looks like you can't reuse connections with ftp, unlike http.

chris