簡體   English   中英

ftp登錄並使用PHP放置文件

[英]ftp log in and put file using PHP

我是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:$ftp_password@$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);
?>

我很快就把這些東西放在一起,請您把它弄糟,請病假;希望它能對您有所幫助。

<?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 }?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM