public function put($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) { $file_size = filesize($local_file_path); $existing_size = 0; if ($resume) { $existing_size = ftp_size($this->conn_id, $remote_file_path); if ($existing_size <= 0) { $resume = false; $existing_size = 0; } else { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("File already exists at remote site: size {$existing_size}. Will attempt resumption."); } if ($existing_size >= $file_size) { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("File is apparently already completely uploaded"); } return true; } } } // From here on, $file_size is only used for logging calculations. We want to avoid divsion by zero. $file_size = max($file_size, 1); if (!($fh = fopen($local_file_path, 'rb'))) { return false; } if ($existing_size) { fseek($fh, $existing_size); } $ret = ftp_nb_fput($this->conn_id, $remote_file_path, $fh, FTP_BINARY, $existing_size); // $existing_size can now be re-purposed while ($ret == FTP_MOREDATA) { if (is_a($updraftplus, 'UpdraftPlus')) { $new_size = ftell($fh); $record_after = 524288; if ($existing_size > 2097152) { $record_after = $existing_size > 4194304 ? 2097152 : 1048576; } if ($new_size - $existing_size > $record_after) { $existing_size = $new_size; $percent = round(100 * $new_size / $file_size, 1); $updraftplus->record_uploaded_chunk($percent, '', $local_file_path); } } // Continue upload $ret = ftp_nb_continue($this->conn_id); } fclose($fh); if ($ret != FTP_FINISHED) { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("FTP upload: error ({$ret})"); } return false; } return true; }
public function putContents($path, $data) { $path = $this->path($path); // Create stack buffer Engine_Stream_Stack::registerWrapper(); $stack = @fopen('stack://tmp', 'w+'); if (!$stack) { throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to create stack buffer')); } // Write into stack $len = 0; do { $tmp = @fwrite($stack, substr($data, $len)); $len += $tmp; } while (strlen($data) > $len && $tmp != 0); // Get mode $mode = $this->getFileMode($path); // Non-blocking mode if (@function_exists('ftp_nb_fput')) { $resource = $this->getResource(); $res = @ftp_nb_fput($resource, $path, $stack, $mode); while ($res == FTP_MOREDATA) { //$this->_announce('nb_get'); $res = @ftp_nb_continue($resource); } $return = $res === FTP_FINISHED; } else { $return = @ftp_fput($this->_handle, $path, $stack, $mode); } // Set umask permission try { $this->mode($path, $this->getUmask(0666)); } catch (Exception $e) { // Silence } if (!$return) { throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to put contents to "%s"', $path)); } return true; }
function ftp_multi_upload($conn_id, $remoteFileName, $backup_file, $mode, $historyID, $tempArgs, $current_file_num = 0) { $requestParams = $this->getRequiredData($historyID, "requestParams"); $task_result = $this->getRequiredData($historyID, "taskResults"); if (!$remoteFileName) { return array('error' => 'Failed to upload file to FTP. Please check your specified path.', 'partial' => 1, 'error_code' => 'failed_to_upload_file_to_ftp'); } $backup_files_base_name = array(); if (is_array($backup_file)) { foreach ($backup_file as $value) { $backup_files_base_name[] = basename($value); } } else { $backup_files_base_name = basename($backup_file); } $backup_files_count = count($backup_file); if (is_array($backup_file)) { $backup_file = $backup_file[$current_file_num]; } $task_result['task_results'][$historyID]['ftp'] = $backup_files_base_name; $task_result['ftp'] = $backup_files_base_name; $backup_settings_values = $this->backup_settings_vals; /* $upload_loop_break_time = $backup_settings_values['upload_loop_break_time']; $del_host_file = $backup_settings_values['del_host_file']; */ $upload_loop_break_time = $requestParams['account_info']['upload_loop_break_time']; //darkcode changed $del_host_file = $requestParams['args']['del_host_file']; if (!$upload_loop_break_time) { $upload_loop_break_time = 25; //safe } $startTime = microtime(true); //get the filesize of the remote file first $file_size = ftp_size($conn_id, $remoteFileName); if ($file_size != -1) { echo "size of {$remoteFileName} is {$file_size} bytes"; } else { $file_size = 0; } if (!$file_size) { $file_size = 0; } //read the parts local file , if it is a second call start reading the file from the left out part which is at the offset of the remote file's filesize. $fp = fopen($backup_file, 'r'); fseek($fp, $file_size); $ret = ftp_nb_fput($conn_id, $remoteFileName, $fp, FTP_BINARY, $file_size); if (!$ret || $ret == FTP_FAILED) { return array('error' => "FTP upload Error. ftp_nb_fput(): Append/Restart not permitted. This feature is required for multi-call backup upload via FTP to work. Please contact your WP site's hosting provider and ask them to fix the problem. You can try dropbox, Amazon S3 or Google Driver as an alternative to it.", 'partial' => 1, 'error_code' => 'ftp_nb_fput_not_permitted_error'); } $resArray = array('status' => 'partiallyCompleted', 'backupParentHID' => $historyID); $result_arr = array(); $result_arr['status'] = 'partiallyCompleted'; $result_arr['nextFunc'] = 'ftp_backup'; $result_arr['ftpArgs'] = $tempArgs; $result_arr['current_file_num'] = $current_file_num; /* 1.run the while loop as long as FTP_MOREDATA is set 2.within the loop if time is greater than specified seconds break the loop and close ftp_con return as "partiallyCompleted" setting nextFunc as ftp_backup. 3.if ret == FTP_FINISHED , it means the ftpUpload is complete .. return as "completed". */ while ($ret == FTP_MOREDATA) { // Do whatever you want $endTime = microtime(true); $timeTaken = $endTime - $this->iwpScriptStartTime; // Continue upload... if ($timeTaken > $upload_loop_break_time) { echo "being stopped --- " . $file_size; $result_arr['timeTaken'] = $timeTaken; $result_arr['file_size_written'] = $file_size; fclose($fp); $this->statusLog($historyID, array('stage' => 'ftpMultiCall', 'status' => 'completed', 'statusMsg' => 'nextCall being stopped --- ', 'nextFunc' => 'ftp_backup', 'task_result' => $task_result, 'responseParams' => $result_arr)); break; } else { $ret = ftp_nb_continue($conn_id); } iwp_mmb_auto_print("ftploop"); } if ($ret != FTP_FINISHED) { fclose($fp); /* if($del_host_file) { @unlink($backup_file); } */ return $resArray; } else { //this is where the backup call ends completing all the uploads $current_file_num += 1; $result_arr['timeTaken'] = $timeTaken; $result_arr['file_size_written'] = $file_size; $result_arr['current_file_num'] = $current_file_num; if ($current_file_num == $backup_files_count) { $result_arr['status'] = 'completed'; $result_arr['nextFunc'] = 'ftp_backup_over'; unset($task_result['task_results'][$historyID]['server']); $resArray['status'] = 'completed'; } else { $result_arr['status'] = 'partiallyCompleted'; $resArray['status'] = 'partiallyCompleted'; } $this->statusLog($historyID, array('stage' => 'ftpMultiCall', 'status' => 'completed', 'statusMsg' => 'nextCall', 'nextFunc' => 'ftp_backup', 'task_result' => $task_result, 'responseParams' => $result_arr)); fclose($fp); //checking file size and comparing $verificationResult = $this->postUploadVerification($conn_id, $backup_file, $remoteFileName, $type = "ftp"); if (!$verificationResult) { return $this->statusLog($historyID, array('stage' => 'uploadFTP', 'status' => 'error', 'statusMsg' => 'FTP verification failed: File may be corrupted.', 'statusCode' => 'ftp_verification_failed_file_maybe_corrupted')); } if ($del_host_file) { @unlink($backup_file); // darkcode testing purpose } iwp_mmb_print_flush('FTP upload: End'); return $resArray; } }
/** * * 从打开的文件到FTP服务器上的文件(非阻塞) * * @param unknown_type $remote_file * @param unknown_type $handle * @param unknown_type $mode * @param unknown_type $startpos */ public static function nbFput($remote_file, $handle, $mode = FTP_ASCII, $startpos = 0) { return ftp_nb_fput(self::$resource, $remote_file, $handle, $mode, $startpos); }
/** * @param $job_object * @return bool */ public function job_run_archive(BackWPup_Job $job_object) { $job_object->substeps_todo = 2 + $job_object->backup_filesize; if ($job_object->steps_data[$job_object->step_working]['SAVE_STEP_TRY'] != $job_object->steps_data[$job_object->step_working]['STEP_TRY']) { $job_object->log(sprintf(__('%d. Try to send backup file to an FTP server …', 'backwpup'), $job_object->steps_data[$job_object->step_working]['STEP_TRY']), E_USER_NOTICE); } if (!empty($job_object->job['ftpssl'])) { //make SSL FTP connection if (function_exists('ftp_ssl_connect')) { $ftp_conn_id = ftp_ssl_connect($job_object->job['ftphost'], $job_object->job['ftphostport'], $job_object->job['ftptimeout']); if ($ftp_conn_id) { $job_object->log(sprintf(__('Connected via explicit SSL-FTP to server: %s', 'backwpup'), $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport']), E_USER_NOTICE); } else { $job_object->log(sprintf(__('Cannot connect via explicit SSL-FTP to server: %s', 'backwpup'), $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport']), E_USER_ERROR); return FALSE; } } else { $job_object->log(__('PHP function to connect with explicit SSL-FTP to server does not exist!', 'backwpup'), E_USER_ERROR); return TRUE; } } else { //make normal FTP connection if SSL not work $ftp_conn_id = ftp_connect($job_object->job['ftphost'], $job_object->job['ftphostport'], $job_object->job['ftptimeout']); if ($ftp_conn_id) { $job_object->log(sprintf(__('Connected to FTP server: %s', 'backwpup'), $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport']), E_USER_NOTICE); } else { $job_object->log(sprintf(__('Cannot connect to FTP server: %s', 'backwpup'), $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport']), E_USER_ERROR); return FALSE; } } //FTP Login $job_object->log(sprintf(__('FTP client command: %s', 'backwpup'), 'USER ' . $job_object->job['ftpuser']), E_USER_NOTICE); if ($loginok = @ftp_login($ftp_conn_id, $job_object->job['ftpuser'], BackWPup_Encryption::decrypt($job_object->job['ftppass']))) { $job_object->log(sprintf(__('FTP server response: %s', 'backwpup'), 'User ' . $job_object->job['ftpuser'] . ' logged in.'), E_USER_NOTICE); } else { //if PHP ftp login don't work use raw login $return = ftp_raw($ftp_conn_id, 'USER ' . $job_object->job['ftpuser']); $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE); if (substr(trim($return[0]), 0, 3) <= 400) { $job_object->log(sprintf(__('FTP client command: %s', 'backwpup'), 'PASS *******'), E_USER_NOTICE); $return = ftp_raw($ftp_conn_id, 'PASS ' . BackWPup_Encryption::decrypt($job_object->job['ftppass'])); if (substr(trim($return[0]), 0, 3) <= 400) { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE); $loginok = TRUE; } else { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), $return[0]), E_USER_ERROR); } } } if (!$loginok) { return FALSE; } //SYSTYPE $job_object->log(sprintf(__('FTP client command: %s', 'backwpup'), 'SYST'), E_USER_NOTICE); $systype = ftp_systype($ftp_conn_id); if ($systype) { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), $systype), E_USER_NOTICE); } else { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), __('Error getting SYSTYPE', 'backwpup')), E_USER_ERROR); } //set actual ftp dir to ftp dir if (empty($job_object->job['ftpdir'])) { $job_object->job['ftpdir'] = trailingslashit(ftp_pwd($ftp_conn_id)); } // prepend actual ftp dir if relative dir if (substr($job_object->job['ftpdir'], 0, 1) != '/') { $job_object->job['ftpdir'] = trailingslashit(ftp_pwd($ftp_conn_id)) . $job_object->job['ftpdir']; } //test ftp dir and create it if not exists if ($job_object->job['ftpdir'] != '/') { @ftp_chdir($ftp_conn_id, '/'); //go to root $ftpdirs = explode('/', trim($job_object->job['ftpdir'], '/')); foreach ($ftpdirs as $ftpdir) { if (empty($ftpdir)) { continue; } if (!@ftp_chdir($ftp_conn_id, $ftpdir)) { if (@ftp_mkdir($ftp_conn_id, $ftpdir)) { $job_object->log(sprintf(__('FTP Folder "%s" created!', 'backwpup'), $ftpdir), E_USER_NOTICE); ftp_chdir($ftp_conn_id, $ftpdir); } else { $job_object->log(sprintf(__('FTP Folder "%s" cannot be created!', 'backwpup'), $ftpdir), E_USER_ERROR); return FALSE; } } } } // Get the current working directory $current_ftp_dir = trailingslashit(ftp_pwd($ftp_conn_id)); if ($job_object->substeps_done == 0) { $job_object->log(sprintf(__('FTP current folder is: %s', 'backwpup'), $current_ftp_dir), E_USER_NOTICE); } //get file size to resume upload @clearstatcache(); $job_object->substeps_done = @ftp_size($ftp_conn_id, $job_object->job['ftpdir'] . $job_object->backup_file); if ($job_object->substeps_done == -1) { $job_object->substeps_done = 0; } //PASV $job_object->log(sprintf(__('FTP client command: %s', 'backwpup'), 'PASV'), E_USER_NOTICE); if ($job_object->job['ftppasv']) { if (ftp_pasv($ftp_conn_id, TRUE)) { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), __('Entering passive mode', 'backwpup')), E_USER_NOTICE); } else { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), __('Cannot enter passive mode', 'backwpup')), E_USER_WARNING); } } else { if (ftp_pasv($ftp_conn_id, FALSE)) { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), __('Entering normal mode', 'backwpup')), E_USER_NOTICE); } else { $job_object->log(sprintf(__('FTP server reply: %s', 'backwpup'), __('Cannot enter normal mode', 'backwpup')), E_USER_WARNING); } } if ($job_object->substeps_done < $job_object->backup_filesize) { $job_object->log(__('Starting upload to FTP  …', 'backwpup'), E_USER_NOTICE); if ($fp = fopen($job_object->backup_folder . $job_object->backup_file, 'rb')) { //go to actual file pos fseek($fp, $job_object->substeps_done); $ret = ftp_nb_fput($ftp_conn_id, $current_ftp_dir . $job_object->backup_file, $fp, FTP_BINARY, $job_object->substeps_done); while ($ret == FTP_MOREDATA) { $job_object->substeps_done = ftell($fp); $job_object->update_working_data(); $job_object->do_restart_time(); $ret = ftp_nb_continue($ftp_conn_id); } if ($ret != FTP_FINISHED) { $job_object->log(__('Cannot transfer backup to FTP server!', 'backwpup'), E_USER_ERROR); return FALSE; } else { $job_object->substeps_done = $job_object->backup_filesize + 1; $job_object->log(sprintf(__('Backup transferred to FTP server: %s', 'backwpup'), $current_ftp_dir . $job_object->backup_file), E_USER_NOTICE); if (!empty($job_object->job['jobid'])) { BackWPup_Option::update($job_object->job['jobid'], 'lastbackupdownloadurl', "ftp://" . $job_object->job['ftpuser'] . ":" . BackWPup_Encryption::decrypt($job_object->job['ftppass']) . "@" . $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport'] . $current_ftp_dir . $job_object->backup_file); } } fclose($fp); } else { $job_object->log(__('Can not open source file for transfer.', 'backwpup'), E_USER_ERROR); return FALSE; } } $backupfilelist = array(); $filecounter = 0; $files = array(); if ($filelist = ftp_nlist($ftp_conn_id, '.')) { foreach ($filelist as $file) { if (basename($file) != '.' && basename($file) != '..') { if ($job_object->is_backup_archive($file)) { $time = ftp_mdtm($ftp_conn_id, $file); if ($time != -1) { $backupfilelist[$time] = basename($file); } else { $backupfilelist[] = basename($file); } } $files[$filecounter]['folder'] = 'ftp://' . $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport'] . $job_object->job['ftpdir']; $files[$filecounter]['file'] = $job_object->job['ftpdir'] . basename($file); $files[$filecounter]['filename'] = basename($file); $files[$filecounter]['downloadurl'] = 'ftp://' . rawurlencode($job_object->job['ftpuser']) . ':' . rawurlencode(BackWPup_Encryption::decrypt($job_object->job['ftppass'])) . '@' . $job_object->job['ftphost'] . ':' . $job_object->job['ftphostport'] . $job_object->job['ftpdir'] . basename($file); $files[$filecounter]['filesize'] = ftp_size($ftp_conn_id, $file); $files[$filecounter]['time'] = ftp_mdtm($ftp_conn_id, $file); $filecounter++; } } } if (!empty($job_object->job['ftpmaxbackups']) && $job_object->job['ftpmaxbackups'] > 0) { //Delete old backups if (count($backupfilelist) > $job_object->job['ftpmaxbackups']) { ksort($backupfilelist); $numdeltefiles = 0; while ($file = array_shift($backupfilelist)) { if (count($backupfilelist) < $job_object->job['ftpmaxbackups']) { break; } if (ftp_delete($ftp_conn_id, $file)) { //delete files on ftp foreach ($files as $key => $filedata) { if ($filedata['file'] == $job_object->job['ftpdir'] . $file) { unset($files[$key]); } } $numdeltefiles++; } else { $job_object->log(sprintf(__('Cannot delete "%s" on FTP server!', 'backwpup'), $job_object->job['ftpdir'] . $file), E_USER_ERROR); } } if ($numdeltefiles > 0) { $job_object->log(sprintf(_n('One file deleted on FTP server', '%d files deleted on FTP server', $numdeltefiles, 'backwpup'), $numdeltefiles), E_USER_NOTICE); } } } set_site_transient('backwpup_' . $job_object->job['jobid'] . '_ftp', $files, YEAR_IN_SECONDS); $job_object->substeps_done++; ftp_close($ftp_conn_id); return TRUE; }
public function put($local_file_path, $remote_file_path, $mode = FTP_BINARY, $resume = false, $updraftplus = false) { $file_size = filesize($local_file_path); $existing_size = 0; if ($resume) { if ($this->curl_handle) { if ($this->curl_handle === true) { $this->connect(); } curl_setopt($this->curl_handle, CURLOPT_URL, 'ftps://' . $this->host . '/' . $remote_file_path); curl_setopt($this->curl_handle, CURLOPT_NOBODY, true); curl_setopt($this->curl_handle, CURLOPT_HEADER, false); // curl_setopt($this->curl_handle, CURLOPT_FORBID_REUSE, true); $getsize = curl_exec($this->curl_handle); if ($getsize) { $sizeinfo = curl_getinfo($this->curl_handle); $existing_size = $sizeinfo['download_content_length']; } else { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("Curl: upload error: " . curl_error($this->curl_handle)); } } } else { $existing_size = ftp_size($this->conn_id, $remote_file_path); } // In fact curl can return -1 as the value, for a non-existant file if ($existing_size <= 0) { $resume = false; $existing_size = 0; } else { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("File already exists at remote site: size {$existing_size}. Will attempt resumption."); } if ($existing_size >= $file_size) { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("File is apparently already completely uploaded"); } return true; } } } // From here on, $file_size is only used for logging calculations. We want to avoid divsion by zero. $file_size = max($file_size, 1); if (!($fh = fopen($local_file_path, 'rb'))) { return false; } if ($existing_size) { fseek($fh, $existing_size); } // FTPS (i.e. implicit encryption) if ($this->curl_handle) { // Reset the curl object (because otherwise we get errors that make no sense) $this->connect(); if (version_compare(phpversion(), '5.3.0', '>=')) { curl_setopt($this->curl_handle, CURLOPT_PROGRESSFUNCTION, array($this, 'curl_progress_function')); curl_setopt($this->curl_handle, CURLOPT_NOPROGRESS, false); } $this->upload_local_path = $local_file_path; $this->upload_last_recorded_percent = 0; $this->upload_size = max($file_size, 1); $this->upload_from = $existing_size; $this->uploaded_bytes = $existing_size; curl_setopt($this->curl_handle, CURLOPT_URL, 'ftps://' . $this->host . '/' . $remote_file_path); if ($existing_size) { curl_setopt($this->curl_handle, CURLOPT_FTPAPPEND, true); } // DOn't set CURLOPT_UPLOAD=true before doing the size check - it results in a bizarre error curl_setopt($this->curl_handle, CURLOPT_UPLOAD, true); curl_setopt($this->curl_handle, CURLOPT_INFILE, $fh); $output = curl_exec($this->curl_handle); fclose($fh); if (is_a($updraftplus, 'UpdraftPlus') && !$output) { $updraftplus->log("FTPS error: " . curl_error($this->curl_handle)); } elseif ($updraftplus === true && !$output) { echo __('Error:', 'updraftplus') . ' ' . curl_error($this->curl_handle) . "\n"; } // Mark as used $this->curl_handle = true; return $output; } $ret = ftp_nb_fput($this->conn_id, $remote_file_path, $fh, FTP_BINARY, $existing_size); // $existing_size can now be re-purposed while ($ret == FTP_MOREDATA) { if (is_a($updraftplus, 'UpdraftPlus')) { $new_size = ftell($fh); if ($new_size - $existing_size > 524288) { $existing_size = $new_size; $percent = round(100 * $new_size / $file_size, 1); $updraftplus->record_uploaded_chunk($percent, '', $local_file_path); } } // Continue upload $ret = ftp_nb_continue($this->conn_id); } fclose($fh); if ($ret != FTP_FINISHED) { if (is_a($updraftplus, 'UpdraftPlus')) { $updraftplus->log("FTP upload: error ({$ret})"); } return false; } return true; }
/** * Uploads from an open file to the FTP server but does not block other operations * * @param string $resource An open file pointer on the local file. * @param string $remotefile Remote path where the file should be stored * @param string $mode Transfer mode for the file. (auto or ascii or binary) * @param integer $position Pointer of the local file from where the upload should be resumed * @return integer 0 = Transfer failed (FTP_FAILED) | 1 = Transfer finished (FTP_FINISHED) | 2 = Transfer in progress (FTP_MOREDATA) */ public function uploadFromFileUnobtrusive($resource, $remotefile, $mode = 'auto', $position = null) { if (is_resource($this->cid)) { $transfermode = self::transferMode($mode); return ftp_nb_fput($this->cid, $remotefile, $resource, $transfermode, $position); } }
/** * This function will upload a file to the ftp-server. You can either specify a absolute * path to the remote-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 from which the file will be uploaded 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 $local_file The local file to upload * @param string $remote_file The absolute or relative path to the file to upload to * @param bool $overwrite (optional) Whether to overwrite existing file * @param int $mode (optional) Either FTP_ASCII or FTP_BINARY * @return mixed True on success, otherwise PEAR::Error * @see NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED */ function fput($local_handle, $remote_file, $overwrite = false, $mode = null) { if (!isset($mode)) { $mode = FTP_BINARY; } $remote_file = $this->_construct_path($remote_file); if (@ftp_size($this->_handle, $remote_file) != -1 && !$overwrite) { return $this->raiseError("Remote file '{$remote_file}' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN); } if (function_exists('ftp_nb_fput')) { $res = @ftp_nb_fput($this->_handle, $remote_file, $local_handle, $mode); while ($res == FTP_MOREDATA) { $this->_announce('nb_put'); $res = @ftp_nb_continue($this->_handle); } } else { $res = @ftp_fput($this->_handle, $remote_file, $local_handle, $mode); } if (!$res) { return $this->raiseError("The File could not be uploaded to '{$remote_file}'.", NET_FTP_ERR_UPLOADFILE_FAILED); } else { return true; } }
/** * Stores a file from an open file to the FTP server (non-blocking) * @link http://php.net/ftp_nb_fput * * @param string $remoteFile The remote file path * @param resource $handle An open file pointer on the local file * @param integer $mode The transfer mode (FTPWrapper::ASCII or FTPWrapper::BINARY) * @param integer $startpos The position in the remote file to start uploading to * @return integer FTPWrapper::FAILED, FTPWrapper::FINISHED or FTPWrapper::MOREDATA */ public function fputNb($remoteFile, $handle, $mode, $startpos = 0) { return ftp_nb_fput($this->connection->getStream(), $remoteFile, $handle, $mode, $startpos); }
public function ftp_nb_fput($conn, $dir, $fp, $mode) { return ftp_nb_fput($conn, $dir, $fp, $mode); }
private function put_large_ftp_file($file){ $url = "ftp://{$this->user}:{$this->pass}@{$this->server}/{$this->directory}/$file"; $path = ABSPATH."simple-backup/".$file; $file_pointer = fopen($path, "rb"); $start = $this->timer(); clearstatcache(); $file_size = filesize($path); $connection = $this->connect(); if($connection === false){ die('Can not Connect to FTP Server'); } if($login = $this->login($connection)){ ftp_pasv($connection,TRUE); $upload_path = $this->find_target_dir($connection); $i = 0; //loop counter $ret = ftp_nb_fput($connection, $file, $file_pointer, FTP_BINARY, FTP_AUTORESUME); while ($ret == FTP_MOREDATA) { $i++; if($i % 500 == 0){ $transfer_progress = ftell($file_pointer); $timer = ($this->timer() - $start); $pct_complete = ($transfer_progress/$file_size) * 100; $percentage_complete = number_format($pct_complete,1); $pct_complete = $percentage_complete."%"; $message = ' %2$s of %3$s Transferred (%1$s Complete) in %4$s Seconds!'; $msg = sprintf( $message, $pct_complete, size_format($transfer_progress,2), size_format($file_size,2), number_format($timer,2) ); $status = array(); $status['message'] = $msg; $status['code'] = "PROCESSING"; $status['percentage'] = $percentage_complete; update_option('simple-backup-file-transfer', $status); echo "."; if($i % 2000 == 0){ flush(); ob_flush(); } } // Continue upload... $ret = ftp_nb_continue($connection); } if ($ret == FTP_FINISHED) { $end = number_format(($this->timer() - $start), 2); $rate = size_format((filesize($path) / $end),2) . "/s"; $message = "Successfully uploaded $file to $upload_path in $end seconds (rate: $rate)"; $status['message'] = $message; $status['code'] = "FINISHED"; $status['percentage'] = 100; update_option('simple-backup-file-transfer', $status); echo "\r\n$message"; } else { $message = "There was a problem while uploading $file to $upload_path '$ret'"; $status['message'] = $message; $status['code'] = "ERROR"; update_option('simple-backup-file-transfer', $status); echo "\r\n$message"; } ftp_close($connection); fclose($file_pointer); } return true; }
<?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));
<?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, ''));
function dest_ftp() { global $WORKING, $STATIC; if (empty($STATIC['JOB']['ftphost']) or empty($STATIC['JOB']['ftpuser']) or empty($STATIC['JOB']['ftppass'])) { $WORKING['STEPSDONE'][] = 'DEST_FTP'; //set done return; } $WORKING['STEPTODO'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']); trigger_error(sprintf(__('%d. try to sending backup file to a FTP Server...', 'backwpup'), $WORKING['DEST_FTP']['STEP_TRY']), E_USER_NOTICE); if ($STATIC['JOB']['ftpssl']) { //make SSL FTP connection if (function_exists('ftp_ssl_connect')) { $ftp_conn_id = ftp_ssl_connect($STATIC['JOB']['ftphost'], $STATIC['JOB']['ftphostport'], 10); if ($ftp_conn_id) { trigger_error(sprintf(__('Connected by SSL-FTP to Server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_NOTICE); } else { trigger_error(sprintf(__('Can not connect by SSL-FTP to Server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_ERROR); return false; } } else { trigger_error(__('PHP function to connect with SSL-FTP to server not exists!', 'backwpup'), E_USER_ERROR); return false; } } else { //make normal FTP connection if SSL not work $ftp_conn_id = ftp_connect($STATIC['JOB']['ftphost'], $STATIC['JOB']['ftphostport'], 10); if ($ftp_conn_id) { trigger_error(sprintf(__('Connected to FTP server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_NOTICE); } else { trigger_error(sprintf(__('Can not connect to FTP server: %s', 'backwpup'), $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport']), E_USER_ERROR); return false; } } //FTP Login $loginok = false; trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' USER ' . $STATIC['JOB']['ftpuser']), E_USER_NOTICE); if ($loginok = ftp_login($ftp_conn_id, $STATIC['JOB']['ftpuser'], backwpup_base64($STATIC['JOB']['ftppass']))) { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), ' User ' . $STATIC['JOB']['ftpuser'] . ' logged in.'), E_USER_NOTICE); } else { //if PHP ftp login don't work use raw login $return = ftp_raw($ftp_conn_id, 'USER ' . $STATIC['JOB']['ftpuser']); trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE); if (substr(trim($return[0]), 0, 3) <= 400) { trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' PASS *******'), E_USER_NOTICE); $return = ftp_raw($ftp_conn_id, 'PASS ' . backwpup_base64($STATIC['JOB']['ftppass'])); trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $return[0]), E_USER_NOTICE); if (substr(trim($return[0]), 0, 3) <= 400) { $loginok = true; } } } if (!$loginok) { return false; } //SYSTYPE trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' SYST'), E_USER_NOTICE); $systype = ftp_systype($ftp_conn_id); if ($systype) { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), $systype), E_USER_NOTICE); } else { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Error getting SYSTYPE', 'backwpup')), E_USER_ERROR); } //test ftp dir and create it f not exists if ($STATIC['JOB']['ftpdir'] != '/' && $STATIC['JOB']['ftpdir'] != '') { $ftpdirs = explode("/", trim($STATIC['JOB']['ftpdir'], '/')); foreach ($ftpdirs as $ftpdir) { if (empty($ftpdir)) { continue; } if (!@ftp_chdir($ftp_conn_id, $ftpdir)) { if (@ftp_mkdir($ftp_conn_id, $ftpdir)) { trigger_error(sprintf(__('FTP folder "%s" created!', 'backwpup'), $ftpdir), E_USER_NOTICE); ftp_chdir($ftp_conn_id, $ftpdir); } else { trigger_error(sprintf(__('FTP folder "%s" can not created!', 'backwpup'), $ftpdir), E_USER_ERROR); return false; } } } } // Get the current working directory $currentftpdir = rtrim(ftp_pwd($ftp_conn_id), '/') . '/'; trigger_error(sprintf(__('FTP current folder is: %s', 'backwpup'), $currentftpdir), E_USER_NOTICE); //delete file on ftp if new try if ($WORKING['STEPDONE'] == 0) { @ftp_delete($ftp_conn_id, $currentftpdir . $STATIC['backupfile']); } //PASV trigger_error(sprintf(__('FTP Client command: %s', 'backwpup'), ' PASV'), E_USER_NOTICE); if ($STATIC['JOB']['ftppasv']) { if (ftp_pasv($ftp_conn_id, true)) { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Entering Passive Mode', 'backwpup')), E_USER_NOTICE); } else { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Can not Entering Passive Mode', 'backwpup')), E_USER_WARNING); } } else { if (ftp_pasv($ftp_conn_id, false)) { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Entering Normal Mode', 'backwpup')), E_USER_NOTICE); } else { trigger_error(sprintf(__('FTP Server reply: %s', 'backwpup'), __('Can not Entering Normal Mode', 'backwpup')), E_USER_WARNING); } } if ($WORKING['STEPDONE'] < $WORKING['STEPTODO']) { trigger_error(__('Upload to FTP now started ... ', 'backwpup'), E_USER_NOTICE); $fp = fopen($STATIC['JOB']['backupdir'] . $STATIC['backupfile'], 'r'); $ret = ftp_nb_fput($ftp_conn_id, $currentftpdir . $STATIC['backupfile'], $fp, FTP_BINARY, $WORKING['STEPDONE']); while ($ret == FTP_MOREDATA) { $WORKING['STEPDONE'] = ftell($fp); update_working_file(); $ret = ftp_nb_continue($ftp_conn_id); } if ($ret != FTP_FINISHED) { trigger_error(__('Can not transfer backup to FTP server!', 'backwpup'), E_USER_ERROR); return false; } else { $WORKING['STEPDONE'] = filesize($STATIC['JOB']['backupdir'] . $STATIC['backupfile']); trigger_error(sprintf(__('Backup transferred to FTP server: %s', 'backwpup'), $currentftpdir . $STATIC['backupfile']), E_USER_NOTICE); $STATIC['JOB']['lastbackupdownloadurl'] = "ftp://" . $STATIC['JOB']['ftpuser'] . ":" . backwpup_base64($STATIC['JOB']['ftppass']) . "@" . $STATIC['JOB']['ftphost'] . ':' . $STATIC['JOB']['ftphostport'] . $currentftpdir . $STATIC['backupfile']; $WORKING['STEPSDONE'][] = 'DEST_FTP'; //set done } fclose($fp); } if ($STATIC['JOB']['ftpmaxbackups'] > 0) { //Delete old backups $backupfilelist = array(); if ($filelist = ftp_nlist($ftp_conn_id, $currentftpdir)) { foreach ($filelist as $files) { if ($STATIC['JOB']['fileprefix'] == substr(basename($files), 0, strlen($STATIC['JOB']['fileprefix'])) and $STATIC['JOB']['fileformart'] == substr(basename($files), -strlen($STATIC['JOB']['fileformart']))) { $backupfilelist[] = basename($files); } } if (sizeof($backupfilelist) > 0) { rsort($backupfilelist); $numdeltefiles = 0; for ($i = $STATIC['JOB']['ftpmaxbackups']; $i < sizeof($backupfilelist); $i++) { if (ftp_delete($ftp_conn_id, $currentftpdir . $backupfilelist[$i])) { //delte files on ftp $numdeltefiles++; } else { trigger_error(sprintf(__('Can not delete "%s" on FTP server!', 'backwpup'), $currentftpdir . $backupfilelist[$i]), E_USER_ERROR); } } if ($numdeltefiles > 0) { trigger_error(sprintf(_n('One file deleted on FTP Server', '%d files deleted on FTP Server', $numdeltefiles, 'backwpup'), $numdeltefiles), E_USER_NOTICE); } } } } ftp_close($ftp_conn_id); $WORKING['STEPDONE']++; }