示例#1
0
function myProcess($values)
{
    global $form;
    $destination = './uploads/';
    // Account FTP on remote server, directory destination, and allows Y/N file overwriting
    $ftp = array('user' => $values['ftpaccount']['U'], 'pass' => $values['ftpaccount']['P'], 'host' => $values['ftpaccount']['H'], 'dest' => $values['ftpdir'], 'overwrite' => (bool) $values['overwrite']);
    $result = 'done';
    $file =& $form->getElement('tstUpload');
    if ($file->isUploadedFile()) {
        $_ftp = new Net_FTP($ftp['host']);
        $ret = $_ftp->connect();
        if (PEAR::isError($ret)) {
            $result = $ret->getMessage();
            // NET_FTP_ERR_CONNECT_FAILED
        } else {
            $ret = $_ftp->login($ftp['user'], $ftp['pass']);
            if (PEAR::isError($ret)) {
                $result = $ret->getMessage();
                // NET_FTP_ERR_LOGIN_FAILED
            } else {
                $_ftp->setPassive();
                $ret = $_ftp->cd($ftp['dest']);
                if (PEAR::isError($ret)) {
                    $result = $ret->getMessage();
                    // NET_FTP_ERR_DIRCHANGE_FAILED
                } else {
                    $fval = $file->getValue();
                    $ret = $_ftp->put($fval['tmp_name'], $fval['name'], $ftp['overwrite']);
                    if (PEAR::isError($ret)) {
                        $result = $ret->getMessage();
                        // NET_FTP_ERR_UPLOADFILE_FAILED
                    }
                }
            }
            $ret = $_ftp->disconnect();
            if (PEAR::isError($ret)) {
                $result = $ret->getMessage();
                // NET_FTP_ERR_DISCONNECT_FAILED
            }
        }
    }
    // write the semaphore to tell progress meter to stop
    // in script 'progressbar.php'
    $semaphore = $destination . $_GET['ID'];
    $fp = fopen($semaphore, 'w', false);
    fwrite($fp, $result);
    fclose($fp);
}
示例#2
0
 function cd($dir)
 {
     if (!PEAR::isError(parent::cd($dir))) {
         $this->_currentwd = $dir;
         return true;
     }
     return false;
 }
示例#3
0
 /**
  * The main entry point method.
  */
 public function main()
 {
     $project = $this->getProject();
     require_once 'Net/FTP.php';
     $ftp = new Net_FTP($this->host, $this->port);
     if ($this->ssl) {
         $ret = $ftp->setSsl();
         if (@PEAR::isError($ret)) {
             throw new BuildException('SSL connection not supported by php' . ': ' . $ret->getMessage());
         } else {
             $this->log('Use SSL connection', $this->logLevel);
         }
     }
     $ret = $ftp->connect();
     if (@PEAR::isError($ret)) {
         throw new BuildException('Could not connect to FTP server ' . $this->host . ' on port ' . $this->port . ': ' . $ret->getMessage());
     } else {
         $this->log('Connected to FTP server ' . $this->host . ' on port ' . $this->port, $this->logLevel);
     }
     $ret = $ftp->login($this->username, $this->password);
     if (@PEAR::isError($ret)) {
         throw new BuildException('Could not login to FTP server ' . $this->host . ' on port ' . $this->port . ' with username ' . $this->username . ': ' . $ret->getMessage());
     } else {
         $this->log('Logged in to FTP server with username ' . $this->username, $this->logLevel);
     }
     if ($this->passive) {
         $this->log('Setting passive mode', $this->logLevel);
         $ret = $ftp->setPassive();
         if (@PEAR::isError($ret)) {
             $ftp->disconnect();
             throw new BuildException('Could not set PASSIVE mode: ' . $ret->getMessage());
         }
     }
     // append '/' to the end if necessary
     $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir . '/';
     if ($this->clearFirst) {
         // TODO change to a loop through all files and directories within current directory
         $this->log('Clearing directory ' . $dir, $this->logLevel);
         $ftp->rm($dir, true);
     }
     // Create directory just in case
     $ret = $ftp->mkdir($dir, true);
     if (@PEAR::isError($ret)) {
         $ftp->disconnect();
         throw new BuildException('Could not create directory ' . $dir . ': ' . $ret->getMessage());
     }
     $ret = $ftp->cd($dir);
     if (@PEAR::isError($ret)) {
         $ftp->disconnect();
         throw new BuildException('Could not change to directory ' . $dir . ': ' . $ret->getMessage());
     } else {
         $this->log('Changed directory ' . $dir, $this->logLevel);
     }
     $fs = FileSystem::getFileSystem();
     $convert = $fs->getSeparator() == '\\';
     foreach ($this->filesets as $fs) {
         // Array for holding directory content informations
         $remoteFileInformations = array();
         $ds = $fs->getDirectoryScanner($project);
         $fromDir = $fs->getDir($project);
         $srcFiles = $ds->getIncludedFiles();
         $srcDirs = $ds->getIncludedDirectories();
         foreach ($srcDirs as $dirname) {
             if ($convert) {
                 $dirname = str_replace('\\', '/', $dirname);
             }
             // Read directory informations, if file exists, else create the directory
             if (!$this->_directoryInformations($ftp, $remoteFileInformations, $dirname)) {
                 $this->log('Will create directory ' . $dirname, $this->logLevel);
                 $ret = $ftp->mkdir($dirname, true);
                 if (@PEAR::isError($ret)) {
                     $ftp->disconnect();
                     throw new BuildException('Could not create directory ' . $dirname . ': ' . $ret->getMessage());
                 }
             }
             if ($this->dirmode) {
                 if ($this->dirmode == 'inherit') {
                     $mode = fileperms($dirname);
                 } else {
                     $mode = $this->dirmode;
                 }
                 // Because Net_FTP does not support a chmod call we call ftp_chmod directly
                 ftp_chmod($ftp->_handle, $mode, $dirname);
             }
         }
         foreach ($srcFiles as $filename) {
             $file = new PhingFile($fromDir->getAbsolutePath(), $filename);
             if ($convert) {
                 $filename = str_replace('\\', '/', $filename);
             }
             $local_filemtime = filemtime($file->getCanonicalPath());
             if (isset($remoteFileInformations[$filename]['stamp'])) {
                 $remoteFileModificationTime = $remoteFileInformations[$filename]['stamp'];
             } else {
                 $remoteFileModificationTime = 0;
             }
             if (!$this->depends || $local_filemtime > $remoteFileModificationTime) {
                 if ($this->skipOnSameSize === true && $file->length() === $ftp->size($filename)) {
                     $this->log('Skipped ' . $file->getCanonicalPath(), $this->logLevel);
                     continue;
                 }
                 $this->log('Will copy ' . $file->getCanonicalPath() . ' to ' . $filename, $this->logLevel);
                 $ret = $ftp->put($file->getCanonicalPath(), $filename, true, $this->mode);
                 if (@PEAR::isError($ret)) {
                     $ftp->disconnect();
                     throw new BuildException('Could not deploy file ' . $filename . ': ' . $ret->getMessage());
                 }
             }
             if ($this->filemode) {
                 if ($this->filemode == 'inherit') {
                     $mode = fileperms($filename);
                 } else {
                     $mode = $this->filemode;
                 }
                 // Because Net_FTP does not support a chmod call we call ftp_chmod directly
                 ftp_chmod($ftp->_handle, $mode, $filename);
             }
         }
     }
     $ftp->disconnect();
     $this->log('Disconnected from FTP server', $this->logLevel);
 }
 public static function ftpSearch(Net_FTP $ftp, $pattern, $path = '/', $stopOnMatch = false)
 {
     self::ftpInclude();
     $results = array();
     // Check pattern type
     if (!is_array($pattern) && !is_string($pattern)) {
         throw new Engine_Package_Exception('invalid pattern');
     }
     $type = is_array($pattern) ? 'list' : ($pattern[0] == '/' ? 'regex' : 'name');
     // Check path
     if (empty($path)) {
         throw new Engine_Package_Exception('invalid path');
     }
     // Change dir
     $ret = $ftp->cd($path);
     if ($ftp->isError($ret)) {
         throw new Engine_Exception($ret->getMessage(), $ret->getCode());
     }
     // List files
     $ret = $ftp->ls();
     if ($ftp->isError($ret)) {
         throw new Engine_Exception($ret->getMessage(), $ret->getCode());
     }
     // Check files
     $dirs = array();
     foreach ($ret as $info) {
         $fullPath = rtrim($path, '/') . '/' . $info['name'];
         // DEBUG
         if ($info['is_dir'] == 'd') {
             $dirs[] = $fullPath;
         }
         switch ($type) {
             case 'list':
                 if (in_array($info['name'], $pattern)) {
                     $results[] = $fullPath;
                     if ($stopOnMatch) {
                         return $results;
                     }
                 }
                 break;
             case 'regex':
                 if (preg_match($pattern, $info['name'])) {
                     // We could use the full path here (to give access to subdirectories)
                     $results[] = $fullPath;
                     if ($stopOnMatch) {
                         return $results;
                     }
                 }
                 break;
             case 'name':
                 if ($pattern == $info['name']) {
                     $results[] = $fullPath;
                     if ($stopOnMatch) {
                         return $results;
                     }
                 }
                 break;
             default:
                 throw new Engine_Package_Exception('invalid pattern');
                 break;
         }
     }
     // Recurse into directories
     foreach ($dirs as $dir) {
         $safeDir = rtrim($dir, '/') . '/';
         // Check to make sure this is correct
         try {
             $childResults = self::ftpSearch($ftp, $pattern, $safeDir);
             $results = array_merge($results, $childResults);
         } catch (Exception $e) {
             continue;
             // @todo should we throw or ignore?
         }
     }
     return $results;
 }
示例#5
0
文件: index.php 项目: pear/net_ftp
head("\$ftp->connect({$host}, {$port})");
Var_Dump::display($ftp->connect());
head("\$ftp->setUsername({$user})");
Var_Dump::display($ftp->setUsername($user));
head("\$ftp->setPassword(xxx)");
Var_Dump::display($ftp->setPassword($pass));
head("\$ftp->login({$user}, xxx)");
Var_Dump::display($ftp->login($user, $pass));
head("\$ftp->pwd()");
Var_Dump::display($ftp->pwd());
head("\$ftp->ls(null, NET_FTP_DIRS_FILES)");
Var_Dump::display($ftp->ls(null, NET_FTP_DIRS_FILES));
head("\$ftp->mkdir({$baseDir})");
Var_Dump::display($ftp->mkdir($baseDir));
head("\$ftp->cd({$baseDir})");
Var_Dump::display($ftp->cd($baseDir));
head("\$ftp->ls(null, NET_FTP_RAWLIST)");
Var_Dump::display($ftp->ls(null, NET_FTP_RAWLIST));
head("\$ftp->put({$baseDir}{$singleTestFile}, {$singleTestFile})");
Var_Dump::display($ftp->put($baseDir . $singleTestFile, $singleTestFile));
head("\$ftp->ls(null, NET_FTP_FILES_ONLY)");
Var_Dump::display($ftp->ls(null, NET_FTP_FILES_ONLY));
head("\$ftp->put({$baseDir}{$singleTestFile}, {$singleTestFile}, true)");
Var_Dump::display($ftp->put($baseDir . $singleTestFile, $singleTestFile, true));
head("\$ftp->ls(null, NET_FTP_FILES_ONLY)");
Var_Dump::display($ftp->ls(null, NET_FTP_FILES_ONLY));
head("\$ftp->mdtm({$singleTestFile}, 'd.m.Y H:i:s')");
Var_Dump::display($ftp->mdtm($singleTestFile, 'd.m.Y'));
head("\$ftp->size({$singleTestFile})");
Var_Dump::display($ftp->size($singleTestFile));
head("\$ftp->get({$singleTestFile}, {$baseDir}{$singleTestFile}, true)");
示例#6
0
 /**
  * The main entry point method.
  */
 public function main()
 {
     $project = $this->getProject();
     require_once 'Net/FTP.php';
     $ftp = new Net_FTP($this->host, $this->port);
     $ret = $ftp->connect();
     if (@PEAR::isError($ret)) {
         throw new BuildException('Could not connect to FTP server ' . $this->host . ' on port ' . $this->port . ': ' . $ret->getMessage());
     } else {
         $this->log('Connected to FTP server ' . $this->host . ' on port ' . $this->port, $this->logLevel);
     }
     $ret = $ftp->login($this->username, $this->password);
     if (@PEAR::isError($ret)) {
         throw new BuildException('Could not login to FTP server ' . $this->host . ' on port ' . $this->port . ' with username ' . $this->username . ': ' . $ret->getMessage());
     } else {
         $this->log('Logged in to FTP server with username ' . $this->username, $this->logLevel);
     }
     if ($this->passive) {
         $this->log('Setting passive mode', $this->logLevel);
         $ret = $ftp->setPassive();
         if (@PEAR::isError($ret)) {
             $ftp->disconnect();
             throw new BuildException('Could not set PASSIVE mode: ' . $ret->getMessage());
         }
     }
     // append '/' to the end if necessary
     $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir . '/';
     if ($this->clearFirst) {
         // TODO change to a loop through all files and directories within current directory
         $this->log('Clearing directory ' . $dir, $this->logLevel);
         $ftp->rm($dir, true);
     }
     // Create directory just in case
     $ret = $ftp->mkdir($dir, true);
     if (@PEAR::isError($ret)) {
         $ftp->disconnect();
         throw new BuildException('Could not create directory ' . $dir . ': ' . $ret->getMessage());
     }
     $ret = $ftp->cd($dir);
     if (@PEAR::isError($ret)) {
         $ftp->disconnect();
         throw new BuildException('Could not change to directory ' . $dir . ': ' . $ret->getMessage());
     } else {
         $this->log('Changed directory ' . $dir, $this->logLevel);
     }
     $fs = FileSystem::getFileSystem();
     $convert = $fs->getSeparator() == '\\';
     foreach ($this->filesets as $fs) {
         $ds = $fs->getDirectoryScanner($project);
         $fromDir = $fs->getDir($project);
         $srcFiles = $ds->getIncludedFiles();
         $srcDirs = $ds->getIncludedDirectories();
         foreach ($srcDirs as $dirname) {
             if ($convert) {
                 $dirname = str_replace('\\', '/', $dirname);
             }
             $this->log('Will create directory ' . $dirname, $this->logLevel);
             $ret = $ftp->mkdir($dirname, true);
             if (@PEAR::isError($ret)) {
                 $ftp->disconnect();
                 throw new BuildException('Could not create directory ' . $dirname . ': ' . $ret->getMessage());
             }
         }
         foreach ($srcFiles as $filename) {
             $file = new PhingFile($fromDir->getAbsolutePath(), $filename);
             if ($convert) {
                 $filename = str_replace('\\', '/', $filename);
             }
             $this->log('Will copy ' . $file->getCanonicalPath() . ' to ' . $filename, $this->logLevel);
             $ret = $ftp->put($file->getCanonicalPath(), $filename, true, $this->mode);
             if (@PEAR::isError($ret)) {
                 $ftp->disconnect();
                 throw new BuildException('Could not deploy file ' . $filename . ': ' . $ret->getMessage());
             }
         }
     }
     $ftp->disconnect();
     $this->log('Disconnected from FTP server', $this->logLevel);
 }
示例#7
0
    die($ret->getMessage());
}
printf('connected at <b>%s</b><br />', $ftp['host']);
//
// 4. login to the FTP server as a well-known user
//
$ret = $f->login($ftp['user'], $ftp['pass']);
if (PEAR::isError($ret)) {
    $f->disconnect();
    die($ret->getMessage());
}
printf('login as <b>%s</b><br />', $ftp['user']);
//
// 5. changes directory to final destination for upload operation
//
$ret = $f->cd($dest);
if (PEAR::isError($ret)) {
    $f->disconnect();
    die($ret->getMessage());
}
//
// 6. attachs an instance of the FTP/Progress subclass observer
//
$observer = new Observer_ProgressUpload($meter);
$ok = $f->attach($observer);
if (!$ok) {
    die('cannot attach a FTP Observer');
}
//
// 7. moves files on the FTP server
//