Follow along with the video below to see how to install our site as a web app on your home screen.
Примечание: This feature may not be available in some browsers.
<?php
///////////////////////////////////////////////////////////////
/**
* Класс копирования файлов с ftp-сервера на сайт.
* Автор файла: niga (izra) izra.ru
* Версия: 1.1
* Дата создания файла: 13 марта 2007 19:46
* Дата редактирования: 13 марта 2007 21:32
*/
///////////////////////////////////////////////////////////////
class ftp_download {
// Лист закачки.
var $file_list = array();
// Настройки ftp.
var $ftp_connection = false;
var $ftp_host;
var $ftp_user;
var $ftp_pass;
var $ftp_folder;
// Локальная папка.
var $local_folder;
// Информация.
var $count_size_total;
var $count_size;
var $count_time;
function ftp_init($ftp_host, $ftp_user, $ftp_pass, $local_folder, $ftp_folder) {
if (empty($ftp_host) or empty($ftp_user) or empty($ftp_pass)) {
echo "Проверьте данные для подключения";
return false;
}
if (empty($local_folder)) {
echo "Необходимо указать папку.";
return false;
}
if (!is_dir($local_folder)) {
echo "Указанной папки не существует.";
return false;
}
if (!is_writeable($local_folder)) {
echo "В текущую папку невозможно записать данные.<br />Укажите права доступа.";
return false;
}
$this->ftp_folder = ereg_replace("/", "", $this->ftp_folder);
$this->local_folder = ($this->local_folder[strlen($this->local_folder) - 1] == '/')?$this->local_folder:$this->local_folder.'/';
$this->ftp_host = $ftp_host;
$this->ftp_user = $ftp_user;
$this->ftp_pass = $ftp_pass;
$this->ftp_folder = $ftp_folder;
$this->local_folder = $local_folder;
if (!$this->open_ftp_conection()) {
echo "Невозможно создать подключение.<br />Проверьте правильность данных для подключения.";
return false;
}
$start_time = $this->get_time();
if (!is_file($this->local_folder.'lock_file')) {
$this->create_list();
$this->create_lock_file();
}
if (is_file($this->local_folder.'lock_file')) {
$this->update_lock_file();
$this->delete_lock_file();
}
$end_time = $this->get_time();
$this->count_time = round($end_time - $start_time, 3);
$this->count_size = $this->convert($this->count_size);
$this->count_size_total = $this->convert($this->count_size_total);
}
function get_time() {
list($seconds, $microSeconds) = explode(' ', microtime());
return ((float)$seconds + (float)$microSeconds);
}
function open_ftp_conection() {
$return = false;
$this->ftp_connection = ftp_connect($this->ftp_host);
if ($this->ftp_connection)
if (ftp_login($this->ftp_connection, $this->ftp_user, $this->ftp_pass))
$return = true;
return $return;
}
function ftp_is_dir($folder) {
if (ftp_chdir($this->ftp_connection, $folder)) {
ftp_chdir($this->ftp_connection, '..');
return true;
} else {
return false;
}
}
function convert($size) {
$unit = array('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb');
if (intval($size) != 0)
$returned = round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $unit[$i];
else
$returned = "0".$unit[0];
return $returned;
}
function get_local_size($file) {
$stat = stat($file);
return $stat[7];
}
function create_list($folder='') {
if ($this->ftp_connection) {
$folder = $this->ftp_folder.$folder;
$files = ftp_rawlist($this->ftp_connection, $folder);
foreach ($files as $ftp_files) {
$file_info = preg_split("/[\s]+/", $ftp_files, 9);
$ftp_files = $file_info[8];
$ftp_size = $file_info[4];
$this->count_size += $ftp_size;
if ($ftp_files != '.' and $ftp_files != '..' and !empty($ftp_files)) {
if (@$this->ftp_is_dir($folder.$ftp_files)) {
if (!is_dir($this->local_folder.$ftp_files)) {
mkdir($this->local_folder.$ftp_files);
chmod($this->local_folder.$ftp_files, 0777);
}
$this->create_list($folder.'/'.$ftp_files);
} else {
$file = ereg_replace("^/", "", $folder.'/'.$ftp_files);
if (!is_file($this->local_folder.$file) or $ftp_size != $this->get_local_size($this->local_folder.$file)) {
$this->file_list[$file] = $ftp_size;
} else {
echo "файл <strong>".$file."</strong> размером <strong> ".$this->convert($ftp_size)."</strong> существует на сервере.<br />";
}
}
}
}
} else {
echo "error: not ftp connection.";
}
}
function create_lock_file() {
$list = '';
foreach ($this->file_list as $file_name => $file_size) {
if (!is_file($this->local_folder.$file_name) or $file_size != $this->get_local_size($this->local_folder.$file_name))
$list .= $file_name.':'.$file_size."\n";
}
$fp = fopen($this->local_folder.'lock_file', "w+");
fwrite($fp, $list);
fclose($fp);
}
function update_lock_file() {
$files = file($this->local_folder.'lock_file');
$this->file_list = array();
foreach ($files as $file) {
$file_info = split(":", $file);
$file_name = $file_info[0];
$file_size = $file_info[1];
if (!is_file($this->local_folder.$file_name) or $file_size != $this->get_local_size($this->local_folder.$file_name)) {
$this->file_list[$file_name] = $file_size;
$start_time = $this->get_time();
if ($this->download_file($file_name, $file_size)) {
$this->update_lock_file();
echo "файл <strong>".$file_name."</strong> размером <strong>".$this->convert($file_size)."</strong> закачан на сервер за ".round($this->get_time() - $start_time, 3).".<br />";
}
}
}
}
function delete_lock_file() {
if (count($this->file_list) == 0) {
unlink($this->local_folder.'lock_file');
}
}
function download_file($file_name, $file_size) {
$returned = false;
if (is_file($this->local_folder.$file_name))
$local_size = $this->get_local_size($this->local_folder.$file_name);
$this->count_size_total += $file_size - $local_size;
if ($local_size != $file_size) {
$download = ftp_nb_get($this->ftp_connection, $this->local_folder.$file_name, $file_name, FTP_BINARY, $local_size);
while ($download == FTP_MOREDATA) {
$download = ftp_nb_continue($this->ftp_connection);
}
if ($download == FTP_FINISHED) {
$returned = true;
}
}
return $returned;
}
}
?>
<?php
set_time_limit(0);
$ftp = new ftp_download();
$ftp->ftp_init('ftp_host', 'ftp_user', 'ftp_pass', 'local_folder', 'ftp_folder');
echo "Общее время загрузки: ".$ftp->count_time."<br />";
echo "Общий объем файлов: ".$ftp->count_size."<br />";
echo "Файлов загружено: ".$ftp->count_size_total."<br />";
?>