Nu har jeg så skrevet koden ind, men den virker ikke. Da jeg skrev dette ind :
protected $_host = null;
protected $_username = null;
protected $_password = null;
protected $_connection = null;
Udskrev den dette:
Fatal error: Uncaught exception 'Exception' with message 'Could not connection to ftp host "host"' in /hsphere/local/home/rune85/data-cms.net/ajax/gem.php:35 Stack trace: #0 /hsphere/local/home/rune85/data-cms.net/ajax/gem.php(52): Ftp->_connect() #1 /hsphere/local/home/rune85/data-cms.net/ajax/gem.php(111): Ftp->setPassiveMode() #2 {main} thrown in /hsphere/local/home/rune85/data-cms.net/ajax/gem.php on line 35.
Nu skriver den den samme fejlkode, når jeg har ændret til dette:
protected $_host = minip;
protected $_username = bruger;
protected $_password = pass;
protected $_connection = null;
Hvad har jeg gjort forkert. Hele koden er stadigvæk den samme:
<?php
class Ftp
{
const FTP_ASCII = 1;
const FTP_BINARY = 2;
protected $_host = "ip";
protected $_username = "bruger";
protected $_password = "pass";
protected $_connection = null;
/**
* Constructor
*
* @param string $host
* @param string $username
* @param string $password
*/
function __construct($host,$username,$password){
$this->_host = $host;
$this->_username = $username;
$this->_password = $password;
}
/**
* Opens a FTP connection
*/
protected function _connect(){
if($this->_connection){
return;
}
$conn = @ftp_connect($this->_host);
if(!$conn){
throw new Exception('Could not connection to ftp host "'.$this->_host.'"');
}
$login = @ftp_login ($conn, $this->_username, $this->_password);
if(!$login){
throw new Exception('Connection established, but could not login.');
}
$this->_connection = $conn;
}
/**
* Sets passive mode for ftp connection. Use whenever server is behind
* a firewall or has trouble connecting to host.
*
* @return Ftp This Ftp object
*/
function setPassiveMode(){
$this->_connect();
ftp_pasv($this->_connection, true);
return $this;
}
/**
* Closes the FTP connection
*/
function close(){
if($this->_connection){
ftp_close($this->_connection);
}
$this->_connection = null;
}
/**
* Stores a local file to FTP host
*
* @param string $source Path to local file
* @param string $target Path to remote file
* @param FTP_BINARY|FTP_ASCII $mode
* @return bool
*/
function put($source,$target,$mode = self::FTP_BINARY){
$this->_connect();
if(!file_exists($source)){
throw new Exception('Could not find local source file "'.$source.'"');
}
if(empty($target)){
throw new Exception('FTP Target filename is empty.');
}
return ftp_put($this->_connection,$target,$source,$mode);
}
/**
* Fetches a file from FTP host to a local file
*
* @param string $source Path to remote file
* @param string $target Path to local file
* @param FTP_BINARY|FTP_ASCII $mode
* @return bool
*/
function get($source,$target,$overwrite = false,$mode = self::FTP_BINARY){
$this->_connect();
if(file_exists($target) && $overwrite !== true){
throw new Exception('Target local file already exists.');
}
if(empty($source)){
throw new Exception('FTP Source file is empty');
}
return ftp_get($this->_connection,$target,$source,$mode);
}
}
// create temporary file
file_put_contents('cache.txt','Some content');
// Ftp handling
$ftp = new Ftp('host','username','password');
$ftp->setPassiveMode();
print $ftp->put('cache.txt','/asdad.test');
print $ftp->get('/asdad.test','cache.txt',true);
$ftp->close();
// Delete temporary file
unlink('cache.txt');
?>
Indlæg senest redigeret d. 12.06.2008 13:39 af Bruger #13210