2012-07-21 34 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 }?>