FTP Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

FTP Functions

Post by Luke »

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)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There was a thread regarding the use of FTP functions in PHP. I know it is around here somewhere, but I can't find it. Try some different search terms and see if you can come up with something.

From what I remember, the anwer was along the lines of 'No', but I can't be certain.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I think it can be used to download files w/a browser but not upload them... anybody know?
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

To the best of my knowledge, you can upload files using FTP in PHP
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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
:lol: ), 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)

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);
	}
	
}
?>
Post Reply