2012-07-21 35 views
0

我是新来的PHP,所以请容易! 我想要做的是以下几点:ftp登录并把文件使用PHP

  • 用户进入一个HTML登录页面并输入他们的详细资料(这些其实都是自己的UNIX长在)
  • 如果这是正确的用户被要求输入一些信息,然后按提交
  • 在此过程中,文件将使用当天的日期创建,并且内容由用户选择。
  • 这个文件是ftp到用户的根目录unix帐户 我应该遵循什么过程来做到这一点?

到目前为止,我的PHP看起来像这样(但它不工作)

<?php 

$ftp_server = '192.168.103.11'; //actual domain is listed here. 
$ftp_username = $_POST['username']; 
$ftp_password = $_POST['password']; 
$location = "ftp://$ftp_username:[email protected]$ftp_server"; 
$dest = '/home/detica/mjm/' 
$source = '/var/www/html/test.txt' 
$mode = 'FTP_ASCII' 

$ftp_connect_id = ftp_connect($ftp_server); 
$ftp_login = ftp_login($ftp_connect_id, $ftp_username, $ftp_password); 
if (! $ftp_connect_id || ! $ftp_login) 
{ 
echo "Unable to connect to $ftp_server"; 
exit; 
} else { 
header("Location: $location"); 
echo "Connected to host $ftp_server"; 

$upload = ftp_put($ftp_connect_id, $dest, $source, $mode); 

if (!$upload) { echo 'FTP upload failed!'; } 

ftp_close(ftp_connect_id); 
} 
ftp_close($ftp_connect_id); 
?> 
+1

你已经跳进游泳池的尽头,现在不是吗? – 2012-07-21 07:19:48

+0

这是很多独立的任务....一个指针让你开始[PHP Curl登录和获取数据](http://stackoverflow.com/q/9455376) – 2012-07-21 07:20:25

+0

为什么你需要FTP? PHP是一种服务器端语言。如果PHP在服务器上,那么文件需要“FTP'd”,为什么FTP呢?为什么不把上传的文件移动到用户的Unix账户? – cegfault 2012-07-21 07:21:43

回答

0

继承人的东西我赶紧把在一起,生病离开你完成它,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>&nbsp;</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>&nbsp;</p> 
    <p><input type="submit" value="Submit"></p> 
</form> 

</div> 
<p>&nbsp;</p> 
</body> 

</html> 
<?php }?>