Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ftp登录并使用PHP放入文件_Php - Fatal编程技术网

ftp登录并使用PHP放入文件

ftp登录并使用PHP放入文件,php,Php,我是PHP新手,所以请轻松一点! 我想做的是: 用户转到html登录页面并输入他们的详细信息(这些实际上是他们的unix登录) 如果这些信息正确,则要求用户输入一些信息并按提交 执行此操作时,将创建一个包含今天日期的文件,内容由用户选择 此文件通过ftp发送到用户unix帐户的根目录 要做到这一点,我应该遵循什么流程 到目前为止,我的PHP看起来像这样(但它不工作) 这是我很快整理好的东西,我让你来完成它;p希望能有帮助 <?php /** * A simple FTP crud

我是PHP新手,所以请轻松一点! 我想做的是:

  • 用户转到html登录页面并输入他们的详细信息(这些实际上是他们的unix登录)
  • 如果这些信息正确,则要求用户输入一些信息并按提交
  • 执行此操作时,将创建一个包含今天日期的文件,内容由用户选择
  • 此文件通过ftp发送到用户unix帐户的根目录 要做到这一点,我应该遵循什么流程
到目前为止,我的PHP看起来像这样(但它不工作)


这是我很快整理好的东西,我让你来完成它;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 }?>

您现在已经跳进了池的最深处,不是吗?这是许多独立的任务。。。。一个指针让你开始为什么你需要FTP?PHP是一种服务器端语言。如果PHP在服务器上,文件需要“FTP”到,为什么要用FTP?为什么不将上传的文件移动到用户的unix帐户?
<?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 }?>