这是我快速整理的东西,我让你完成它;p希望它有帮助。
<?php
/**
* A simple FTP crud class
*/
Class ftp_it{
public $status;
function __construct($host,$user,$pass){
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->status = 'Ready';
}
/*Singleton FTP Connect*/
private function connect(){
if (!isset($this->ftp)){
$this->ftp = ftp_connect($this->host, 21, 3) or die ("Cannot connect to host");
ftp_login($this->ftp, $this->user, $this->pass) or die("Cannot login");
ftp_pasv($this->ftp, true);
$this->status = 'Connected';
}
}
public function get($local_file,$ftp_path){
$this->connect();
if(ftp_get($this->ftp, $local_file, $ftp_path, FTP_BINARY)) {
$this->status = 'Download complete';
}else{
$this->status = 'Cannot download';
}
}
public function put($local_file,$ftp_path){
$this->connect();
if(ftp_put($this->ftp, $ftp_path, $local_file, FTP_BINARY)) {
$this->status = 'Upload complete';
}else{
$this->status = 'Cannot upload';
}
}
public function delete($ftp_path){
$this->connect();
if (ftp_delete($this->ftp, $ftp_path)) {
$this->status = "$ftp_path deleted successful";
}else{
$this->status = "Could not delete $ftp_path";
}
}
public function make_dir($dir){
$this->connect();
if (ftp_mkdir($this->ftp, $dir)) {
$this->status = "Successfully created $dir";
} else {
$this->status = "Could not create $dir";
}
}
public function delete_dir($dir){
$this->connect();
if (ftp_rmdir($this->ftp, $dir)) {
$this->status = "Successfully deleted $dir\n";
} else {
$this->status = "Could not delete $dir\n";
}
}
public function show_files($dir='/'){
$this->connect();
return ftp_nlist($this->ftp, $dir);
}
private function close(){
ftp_close($this->ftp);
}
function __destruct(){
if(isset($this->ftp)){
$this->close();
}
}
}//END Class
//Has user posted form?
if($_SERVER['REQUEST_METHOD']=='POST'){
//Assign values from form ill leave you to workout validation
$content = $_POST['content'];
$filename = $_POST['fn'];
//Host creds
$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
//Start FTP crud
$ftp = new ftp_it($host,$user,$pass);
//Some other options ;)
//$ftp->get('./DOWN/test.txt','/test.txt');
//$ftp->delete('/test.txt');
//$ftp->make_dir('/test');
//$ftp->delete_dir('/test');
//$ftp->show_files('/');
//Create A temp file for the POSTEd Contents
$tmpfname = tempnam("/tmp", "FTP");
$handle = fopen($tmpfname, "w");
//Write it to file
fwrite($handle, $content);
fclose($handle);
//Upload the file
$ftp->put($tmpfname,'/'.$filename);
//Status
echo $ftp->status;
}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Save this to my FTP</title>
<style>
*{padding:0px; margin:0px;}
#outer{padding:5px;}
</style>
</head>
<body topmargin="0" leftmargin="0">
<div id="outer">
<form method="POST" action="">
<h1>Save this to my FTP</h1>
<p>The Content:</p>
<p><textarea rows="8" name="content" cols="62"></textarea></p>
<p>Filename: <input type="text" name="fn" size="22"></p>
<p> </p>
<p>
Host: <input type="text" name="host" size="15">
Username: <input type="text" name="user" size="14">
Password: <input type="text" name="pass" size="14">
</p>
<p> </p>
<p><input type="submit" value="Submit"></p>
</form>
</div>
<p> </p>
</body>
</html>
<?php }?>