function query($local_file, $remote_file) { if (!self::islogin()) { if (!self::login()) { return 0; } } self::$temp_file = $local_file . self::$suffix; self::$local_file = $local_file; //self::$ret = @ftp_nb_get(self::$cid, self::$temp_file, self::$root.$remote_file, FTP_BINARY); self::$fp = fopen(self::$temp_file, 'wb'); self::$ret = @ftp_nb_fget(self::$cid, self::$fp, self::$root . $remote_file, FTP_BINARY); return self::$ret != FTP_FAILED; }
/** * XU_Ftp::download_xu() * * Download a file * * @access public * @param string $remote_file Remote filename * @param string $fid File ID * @param bool $max_size Max file size * @return bool */ public function download_xu($remote_file, $fid, $max_size) { if (!$this->_is_conn()) { return FALSE; } $result = TRUE; // if no filepointer is given, make a temp file and open it for writing $tmpfname = tempnam(ROOTPATH . '/temp', "RFT-"); $l_fp = fopen($tmpfname, "wb"); // Initate the download $i = 0; $CI =& get_instance(); $ret = ftp_nb_fget($this->conn_id, $l_fp, $remote_file, FTP_BINARY); while ($ret == FTP_MOREDATA) { if ($i % 10 == 0) { $fstat = fstat($l_fp); $p = $fstat[7]; $CI->db->where('fid', $fid); $CI->db->update('progress', array('progress' => $p, 'curr_time' => time())); } // Continue downloading... $ret = ftp_nb_continue($this->conn_id); $i++; } // Has it finished completly? if ($ret != FTP_FINISHED) { log_message('error', "FTP TRANSFER FAILED"); $result = FALSE; } if ($result === FALSE) { if ($this->debug == TRUE) { $msg = 'ftp_unable_to_download'; $this->_error($msg); } else { $this->error = TRUE; $this->error_msg = 'ftp_unable_to_download'; } return FALSE; } return $tmpfname; }
/** * 从检索FTP服务器上的文件并将其写入一个打开的文件(非阻塞) * * @param resource $handle * @param string $remote_file * @param int $mode * @param int $resumepos * @return int 返回ftp_failed或ftp_finished或ftp_moredata */ public static function nbFget($handle, $remote_file, $mode = FTP_ASCII, $resumepos = 0) { return ftp_nb_fget(self::$resource, $handle, $remote_file, $mode, $resumepos); }
public function get($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) { $file_last_size = 0; if ($resume) { if (!($fh = fopen($local_file_path, 'ab'))) { return false; } clearstatcache($local_file_path); $file_last_size = filesize($local_file_path); } else { if (!($fh = fopen($local_file_path, 'wb'))) { return false; } } // Implicit FTP, for which we use curl (since PHP's native FTP functions don't handle implicit FTP) if ($this->curl_handle) { if ($resume) { curl_setopt($this->curl_handle, CURLOPT_RESUME_FROM, $resume); } curl_setopt($this->curl_handle, CURLOPT_NOBODY, false); curl_setopt($this->curl_handle, CURLOPT_URL, 'ftps://' . $this->host . '/' . $remote_file_path); curl_setopt($this->curl_handle, CURLOPT_UPLOAD, false); curl_setopt($this->curl_handle, CURLOPT_FILE, $fh); $output = curl_exec($this->curl_handle); if ($output) { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch complete"); } } else { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch failed"); } } return $output; } $ret = ftp_nb_fget($this->conn_id, $fh, $remote_file_path, $mode, $file_last_size); if (false == $ret) { return false; } while ($ret == FTP_MOREDATA) { if ($updraftplus) { $file_now_size = filesize($local_file_path); if ($file_now_size - $file_last_size > 524288) { $updraftplus->log("FTP fetch: file size is now: " . sprintf("%0.2f", filesize($local_file_path) / 1048576) . " Mb"); $file_last_size = $file_now_size; } clearstatcache(); } $ret = ftp_nb_continue($this->conn_id); } fclose($fh); if ($ret == FTP_FINISHED) { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch complete"); } return true; } else { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch failed"); } return false; } }
/** * Downloads a file from the FTP server and saves to an open file but does not block other operations * * @param string $remotefile Remote path of the file to download * @param string $resource Resource handle of the open file in which the downloaded file should be written * @param string $mode Transfer mode for the file. (auto or ascii or binary) * @param integer $position Pointer of the remote file from where the download should be resumed * @return integer 0 = Transfer failed (FTP_FAILED) | 1 = Transfer finished (FTP_FINISHED) | 2 = Transfer in progress (FTP_MOREDATA) */ public function downloadToFileUnobtrusive($remotefile, $resource, $mode = 'auto', $position = null) { if (is_resource($this->cid)) { $transfermode = self::transferMode($mode); return ftp_nb_fget($this->cid, $resource, $remotefile, $transfermode, $position); } }
/** * This function will download a file from the ftp-server. You can either spcify a absolute * path to the file (beginning with "/") or a relative one, which will be completed * with the actual directory you selected on the server. You can specify * the path to which the file will be downloaded on the local * maschine, if the file should be overwritten if it exists (optionally, default is * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be * downloaded (if you do not specify this, the method tries to determine it automatically * from the mode-directory or uses the default-mode, set by you). If you give a relative * path to the local-file, the script-path is used as basepath. * * @access public * @param string $remote_file The absolute or relative path to the file to download * @param handle $local_hanlde The local file pointer to put the downloaded in * @param int $mode (optional) Either FTP_ASCII or FTP_BINARY * @return mixed True on success, otherwise PEAR::Error * @see NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED */ function fget($remote_file, $local_handle, $mode = null) { if (!isset($mode)) { $mode = $this->checkFileExtension($remote_file); } $remote_file = $this->_construct_path($remote_file); if (@function_exists('ftp_nb_fget')) { $res = @ftp_nb_fget($this->_handle, $local_handle, $remote_file, $mode); while ($res == FTP_MOREDATA) { $this->_announce('nb_fget'); $res = @ftp_nb_continue($this->_handle); } } else { $res = @ftp_fget($this->_handle, $local_handle, $remote_file, $mode); } if (!$res) { return $this->raiseError("File '{$remote_file}' could not be downloaded.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED); } else { return true; } }
<?php require 'server.inc'; $ftp = ftp_connect('127.0.0.1', $port); ftp_login($ftp, 'user', 'pass'); if (!$ftp) { die("Couldn't connect to the server"); } $local_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "ftp_nb_fget_basic3.txt"; file_put_contents($local_file, 'ASCIIFoo'); $handle = fopen($local_file, 'a'); var_dump(ftp_nb_fget($ftp, $handle, 'fgetresume.txt', FTP_ASCII, 8)); var_dump(file_get_contents($local_file)); @unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . "ftp_nb_fget_basic3.txt");
<?php $bogus = 1; require 'server.inc'; $ftp = ftp_connect('127.0.0.1', $port); if (!$ftp) { die("Couldn't connect to the server"); } var_dump(ftp_login($ftp, 'anonymous', '*****@*****.**')); var_dump(ftp_alloc($ftp, 400)); var_dump(ftp_cdup($ftp)); var_dump(ftp_chdir($ftp, '~')); var_dump(ftp_chmod($ftp, 0666, 'x')); var_dump(ftp_delete($ftp, 'x')); var_dump(ftp_exec($ftp, 'x')); var_dump(ftp_fget($ftp, STDOUT, 'x', 0)); var_dump(ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0)); var_dump(ftp_get($ftp, 'x', 'y', 0)); var_dump(ftp_mdtm($ftp, 'x')); var_dump(ftp_mkdir($ftp, 'x')); var_dump(ftp_nb_continue($ftp)); var_dump(ftp_nb_fget($ftp, STDOUT, 'x', 0)); var_dump(ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0)); var_dump(ftp_systype($ftp)); var_dump(ftp_pwd($ftp)); var_dump(ftp_size($ftp, '')); var_dump(ftp_rmdir($ftp, ''));
public function getContents($path) { $path = $this->path($path); // Create stack buffer Engine_Stream_Stack::registerWrapper(); $stack = fopen('stack://tmp'); if (!$stack) { throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to create stack buffer')); } // Get mode $mode = $this->getFileMode($path); // Non-blocking mode if (@function_exists('ftp_nb_fget')) { $resource = $this->getResource(); $res = @ftp_nb_fget($resource, $stack, $path, $mode); while ($res == FTP_MOREDATA) { //$this->_announce('nb_get'); $res = @ftp_nb_continue($resource); } $return = $res === FTP_FINISHED; } else { $return = @ftp_fget($this->_handle, $stack, $path, $mode); } if (!$return) { throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to get contents of "%s"', $path)); } $data = ''; while (false != ($dat = fread($stack, 1024))) { $data .= $dat; } return $data; }
public function get($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) { $file_last_size = 0; if ($resume) { if (!($fh = fopen($local_file_path, 'ab'))) { return false; } clearstatcache($local_file_path); $file_last_size = filesize($local_file_path); } else { if (!($fh = fopen($local_file_path, 'wb'))) { return false; } } $ret = ftp_nb_fget($this->conn_id, $fh, $remote_file_path, $mode, $file_last_size); if (false == $ret) { return false; } while ($ret == FTP_MOREDATA) { if ($updraftplus) { $file_now_size = filesize($local_file_path); if ($file_now_size - $file_last_size > 524288) { $updraftplus->log("FTP fetch: file size is now: " . sprintf("%0.2f", filesize($local_file_path) / 1048576) . " Mb"); $file_last_size = $file_now_size; } clearstatcache($local_file_path); } $ret = ftp_nb_continue($this->conn_id); } fclose($fh); if ($ret == FTP_FINISHED) { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch complete"); } return true; } else { if ($updraftplus) { $updraftplus->log("FTP fetch: fetch failed"); } return false; } }
public function ftp_nb_fget($conn, $fp, $file, $mode) { return ftp_nb_fget($conn, $fp, $file, $mode); }
<?php $ftp = null; var_dump(ftp_connect(array())); var_dump(ftp_connect('127.0.0.1', 0, -3)); var_dump(ftp_raw($ftp)); var_dump(ftp_mkdir($ftp)); var_dump(ftp_rmdir($ftp)); var_dump(ftp_nlist($ftp)); var_dump(ftp_rawlist($ftp)); var_dump(ftp_fget($ftp)); var_dump(ftp_nb_fget($ftp)); var_dump(ftp_nb_get($ftp)); var_dump(ftp_pasv($ftp)); var_dump(ftp_nb_continue()); var_dump(ftp_fput()); var_dump(ftp_nb_fput($ftp)); var_dump(ftp_put($ftp)); var_dump(ftp_nb_put($ftp)); var_dump(ftp_size($ftp)); var_dump(ftp_mdtm($ftp)); var_dump(ftp_rename($ftp)); var_dump(ftp_site($ftp)); var_dump(ftp_set_option($ftp)); var_dump(ftp_get_option($ftp));
/** * Download a file * */ function download_xu2($remote_file, $fid, $max_size) { if (!$this->_is_conn()) { return FALSE; } $result = TRUE; // TODO: Make this use cache. $tmpfname = tempnam('./temp', 'RFT-'); $l_fp = fopen($tmpfname, "wb"); $i = 0; $CI =& get_instance(); $ret = ftp_nb_fget($this->conn_id, $l_fp, $remote_file, FTP_BINARY); while ($ret == FTP_MOREDATA) { if ($i % 10 == 0) { $fstat = fstat($l_fp); $p = $fstat[7]; $CI->db->where('fid', $fid); $CI->db->update('progress', array('progress' => $p, 'curr_time' => time())); } // Continue downloading... $ret = ftp_nb_continue($this->conn_id); $i++; } if ($ret != FTP_FINISHED) { log_message('error', 'FTP TRANSFER FAILED'); $result = FALSE; } if ($result === FALSE) { if ($this->debug == TRUE) { $msg = 'ftp_unable_to_download'; $this->_error($msg); } else { $this->error = TRUE; $this->error_msg = 'ftp_unable_to_download'; } return FALSE; } return $tmpfname; }
public function download() { // remove session if (isset($_SESSION['showDownload'])) { // reset session variable for next time $_SESSION['showDownload'] = null; unset($_SESSION['showDownload']); session_write_close(); } // php script timeout for long downloads (2 days!) set_time_limit(60 * 60 * 24 * 2); // load the server the file is on $storageType = 'local'; $storageLocation = _CONFIG_FILE_STORAGE_PATH; $uploadServerDetails = $this->loadServer(); if ($uploadServerDetails != false) { $storageLocation = $uploadServerDetails['storagePath']; $storageType = $uploadServerDetails['serverType']; // if no storage path set & local, use system default if (strlen($storageLocation) == 0 && $storageType == 'local') { $storageLocation = _CONFIG_FILE_STORAGE_PATH; } } // get file path $fullPath = $this->getFullFilePath($storageLocation); // open file - via ftp if ($storageType == 'remote') { // connect via ftp $conn_id = ftp_connect($uploadServerDetails['ipAddress'], $uploadServerDetails['ftpPort'], 30); if ($conn_id === false) { $this->errorMsg = 'Could not connect to ' . $uploadServerDetails['ipAddress'] . ' to upload file.'; return false; } // authenticate $login_result = ftp_login($conn_id, $uploadServerDetails['ftpUsername'], $uploadServerDetails['ftpPassword']); if ($login_result === false) { $this->errorMsg = 'Could not login to ' . $uploadServerDetails['ipAddress'] . ' with supplied credentials.'; return false; } // prepare the stream of data $pipes = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); if ($pipes === false) { $this->errorMsg = 'Could not create stream to download file on ' . $uploadServerDetails['ipAddress']; return false; } stream_set_write_buffer($pipes[0], 10000); stream_set_timeout($pipes[1], 10); stream_set_blocking($pipes[1], 0); $fail = false; $ret = ftp_nb_fget($conn_id, $pipes[0], $fullPath, FTP_BINARY, FTP_AUTORESUME); } else { $handle = @fopen($fullPath, "r"); if (!$handle) { $this->errorMsg = 'Could not open file for reading.'; return false; } } // download speed $speed = 0; // if free/non user $Auth = Auth::getAuth(); if ($Auth->loggedIn == false || $Auth->level == 'free user') { $speed = (int) SITE_CONFIG_FREE_USER_MAX_DOWNLOAD_SPEED; } else { $speed = (int) SITE_CONFIG_PREMIUM_USER_MAX_DOWNLOAD_SPEED; } // do we need to throttle the speed? if ($speed > 0) { // create new throttle config $config = new ThrottleConfig(); // set standard transfer rate (in bytes/second) $config->burstLimit = $speed; $config->rateLimit = $speed; // enable module (this is a default value) $config->enabled = true; // start throttling $x = new Throttle($config); } // output some headers header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-type: " . $this->fileType); header("Pragma: public"); header("Content-Disposition: attachment; filename=\"" . str_replace("\"", "", $this->originalFilename) . "\""); header("Content-Description: File Transfer"); header("Content-Length: " . $this->fileSize); // output file - via ftp if ($storageType == 'remote') { while ($ret == FTP_MOREDATA) { $contents = stream_get_contents($pipes[1]); if ($contents !== false) { echo $contents; flush(); } $ret = ftp_nb_continue($conn_id); } /* $contents = stream_get_contents($pipes[1]); if($contents !== false) { echo $contents; flush(); } */ fclose($pipes[0]); fclose($pipes[1]); } else { while (($buffer = fgets($handle, 4096)) !== false) { echo $buffer; } fclose($handle); } exit; }
<?php require 'server.inc'; $ftp = ftp_connect('127.0.0.1', $port); ftp_login($ftp, 'user', 'pass'); if (!$ftp) { die("Couldn't connect to the server"); } ftp_set_option($ftp, FTP_AUTOSEEK, false); $local_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . "ftp_nb_fget_basic1.txt"; $handle = fopen($local_file, 'w'); var_dump(ftp_nb_fget($ftp, $handle, 'fget.txt', FTP_ASCII, FTP_AUTORESUME)); var_dump(file_get_contents($local_file));
/** * Retrieves a remote file and writes it ton an open file (non-blocking) * @link http://php.net/ftp_nb_fget * * @param resource $handle An open file pointer in which we store the data * @param string $remoteFile The remote file path * @param integer $mode The transfer mode (FTPWrapper::ASCII or FTPWrapper::BINARY) * @param integer $resumepos The position in the remote file to start downloading from * @return integer FTPWrapper::FAILED, FTPWrapper::FINISHED or FTPWrapper::MOREDATA */ public function fgetNb($handle, $remoteFile, $mode = self::BINARY, $resumepos = 0) { return ftp_nb_fget($this->connection->getStream(), $handle, $remoteFile, $mode, $resumepos); }
/** * @param $localFile * @param $remoteFile * @param int $mode * @return bool */ public function getAsynchronously($localFile, $remoteFile, $mode = FTP_ASCII) { $localFilRes = fopen($localFile, 'w'); return ftp_nb_fget($this->getFtp(), $localFilRes, $remoteFile, $mode); }
/** Reads the log. * This function reads all the remaining log since last read (paying no attention to line returns). It also downloads it from an FTP server if specified. * * \return The read data if averything passed correctly, FALSE otherwise. If no data has been read, it returns an empty string. */ public function readLog() { switch ($this->_logfile['type']) { case 'ftp': //If we have finished to download the log, we read it and restart downloading if ($this->_logfile['state'] == FTP_FINISHED) { $data = ''; //We read only if the pointer has changed, i.e. something has been downloaded. if ($this->_logfile['pointer'] != ftell($this->_logfile['fp'])) { fseek($this->_logfile['fp'], $this->_logfile['pointer']); //Reset file pointer to last read position (ftp_nb_fget moves the pointer) $data = ''; $read = NULL; while ($read === NULL || $read) { $read = fread($this->_logfile['fp'], 1024); $data .= $read; } $this->_logfile['pointer'] = ftell($this->_logfile['fp']); //Save new pointer position } //Calculation of new global pointer and sending command $totalpointer = $this->_logfile['pointer'] + $this->_logfile['origpointer']; $this->_logfile['state'] = ftp_nb_fget($this->_logfile['ftp'], $this->_logfile['fp'], $this->_logfile['location'], FTP_BINARY, $totalpointer); return $data; } elseif ($this->_logfile['state'] == FTP_FAILED) { Leelabot::message('Can\'t read the remote FTP log anymore.', array(), E_WARNING); $this->openLogFile(TRUE); return FALSE; } else { $this->_logfile['state'] = ftp_nb_continue($this->_logfile['ftp']); } break; case 'file': $data = ''; $read = NULL; while ($read === NULL || $read) { $read = fread($this->_logfile['fp'], 1024); $data .= $read; } return $data; break; } }
/** * Read the previous scan results from the file system. * * @return array */ public function read() { if (!$this->handle) { return false; } $stream = fopen('php://memory', 'w'); if (!$stream) { return false; } $data = ''; if ($ret = ftp_nb_fget($this->handle, $stream, $this->filename, $this->transfer_mode)) { while ($ret === FTP_MOREDATA) { rewind($stream); $data .= stream_get_contents($stream); $ret = ftp_nb_continue($this->handle); } if ($ret != FTP_FINISHED) { return false; } else { $data = (array) Yaml::parse($data); return Report\Report::fromArray($data); } } return false; }