FTP Functions
Moderator: General Moderators
FTP Functions
Could these be used to build an FTP app that works on a site? (Sorry if this is a stupid question, it just seems like if they can, more File management apps would be built with these functions... and I haven't seen even one)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I HOPE THIS ISN'T CONSIDERED A BUMP:
Just for those who need to know the answer to this... the FTP functions are for connecting from your server to another remote server somewhere. For files that are on your server, use PHP's file functions. For some reason I was thinking that this would allow larger file uploads or something (I was thinking the transfer of files from server to client would be FTP, instead of HTTP, because I wasn't really thinking
), but I misunderstood what these functions do. You still would have to POST the file to upload or write the file to the browser to download via HTTP, but with these functions, you could then move that file to another server from yours or request files from another server to yours.
I'm not sure why that hadn't occured to me... but I understand now.
EDIT: None-the-less, I wrote this (which is by no means finished or refined, but it works)
Just for those who need to know the answer to this... the FTP functions are for connecting from your server to another remote server somewhere. For files that are on your server, use PHP's file functions. For some reason I was thinking that this would allow larger file uploads or something (I was thinking the transfer of files from server to client would be FTP, instead of HTTP, because I wasn't really thinking
I'm not sure why that hadn't occured to me... but I understand now.
EDIT: None-the-less, I wrote this (which is by no means finished or refined, but it works)
Code: Select all
<?php
class ftp{
private $conn;
private $host;
private $username;
private $password;
private $port;
private $timeout;
private $logged_in;
private $dir;
private $dir_contents;
public function __construct($host, $username, $password, $dir=false, $port=false, $timeout=false, $secure=false){
// Set login credentials
$this->host = $host;
$this->username = $username;
$this->password = $password;
// Set port number, and if not present, connect to 21
$this->port = $port ? $port : 21;
// Connect to FTP server
$this->conn = $secure ? ftp_ssl_connect($this->host, $this->port) : ftp_connect($this->host, $this->port);
//$this->timeout = $timeout ? $timeout : ftp_get_option($this->conn, FTP_TIMEOUT_SEC);
$this->login();
$this->dir = $dir ? $dir : "/";
$this->load_dir_contents();
}
private function login(){
$this->logged_in = ftp_login($this->conn, $this->username, $this->password);
}
public function is_logged_in(){
return $this->logged_in;
}
// Returns current working directory
public function get_cwd(){
return ftp_pwd($this->conn);
}
// Opens directory relative to current directory
public function open_dir($dir){
$dir = $this->dir . '/' . $dir;
if($this->in_dir($dir)){
if($this->goto($dir)){
$this->load_dir_contents();
return true;
}
}
return false;
}
// Goes to directory by full pathname
public function goto($dir){
if(ftp_chdir($this->conn, $dir)){
$this->load_dir_contents();
return true;
}
return false;
}
// Move up one directory
public function up_dir(){
ftp_cdup($this->conn);
$this->load_dir_contents();
}
// Checks if something is in a directory
public function in_dir($dir){
return in_array($dir, $this->dir_contents);
}
public function is_dir($dir){
$contents = ftp_nlist($this->conn, $dir);
if (!empty($contents[0])) {
$check = str_replace($dir, "", $contents[0]);
if (!empty($check)) {
return true;
}
}
}
// Loads contents of the current directory
private function load_dir_contents(){
$this->dir = $this->get_cwd();
$this->dir_contents = ftp_nlist($this->conn, $this->dir);
}
// For debugging
public function print_dir(){
echo "<pre>DIR:";
print_r($this->dir);
echo "<br />CONTENTS:";
print_r($this->dir_contents);
echo "</pre>";
}
public function link_view(){
$html = "";
foreach($this->dir_contents as $val){
if($this->is_dir($val)){
$dirlen = strlen($this->dir);
$html .= "<p><a href='?dir=" . urlencode($val) . "'>" . substr($val, $dirlen) . "</a></p>";
}
}
return $html;
}
// Closes ftp connection
public function __destruct(){
ftp_close($this->conn);
}
}
?>