<?php
// set up basic connection
$conn_id = ftp_connect("$ftp_server"); // login with username and password
$login_result = ftp_login($conn_id, "$ftp_user_name", "$ftp_user_pass"); // check connection
if ((!$conn_id) || (!$login_result)) { 
        echo "Ftp connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        die; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }// upload the file
$upload = ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY); // check upload status
if (!$upload) { 
        echo "Ftp upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }// close the FTP stream 
ftp_quit($conn_id); 
?>

解决方案 »

  1.   

    〈?php
      $username="用户名";
      $password="用户密码";
      $server="主机名";
      $cdir="上传目录名" ;
    //以上设置你的FTP主机名、用户名和用户密码
      ?〉
    〈!-- 文件上传设置标签 --〉
      〈form enctype="multipart/form-data" action=ftp.php method=post〉
    〈!-- 传递变量 --〉
        〈input type=hidden name=username value=〈? echo $username; ?〉〉
        〈input type=hidden name=password value=〈? echo $password; ?〉〉
        〈input type=hidden name=server value=〈? echo $server; ?〉〉
        〈input type=hidden name=cdir value=〈? echo $cdir; ?〉〉
      〈table〉
      〈tr〉
        〈td〉上传文件选择
        〈input type=file name=upfile〉
        〈/td〉
      〈/tr〉
      〈tr〉
        〈td〉
    〈!-- 提交表单 --〉
        〈input type=submit name=action value=上传〉
        〈/td〉
      〈/tr〉
      〈/table〉
      〈/form〉处理上传文件:ftp.php 代码如下:〈?php
    //ftp联接主机函数
      function connect()
      {
      global $server, $username, $password;
      $conn = ftp_connect($server);
      ftp_login($conn, $username, $password);
      return $conn;
      }
    //建立ftp联接
      $result = connect();
      if ($action == "上传")
      {
    //用来改变ftp路径
      ftp_chdir($result, $cdir);
    //用来上传指定的文件,同名并以二进制位传递
      $res_code = ftp_put($result, $upfile_name, $upfile, FTP_BINARY);
    // 判断是否正确上传
      if ($res_code == 1)
    echo "上传成功!";
      else
    echo "上传错误!";
      }
    // 关闭联接
      ftp_quit($result);
      ?〉
      

  2.   

    谢谢 wasy(嘻嘻哈哈) 和 limenghao2(梦豪) ,但是我不想通过<form>,如何操作客户端本地的文件,例如客户输入本地的目录名,我就把它全部上载,但是我不知道如何访问本地的文件,通过你们说的方法传到服务器的指定目录???
      

  3.   

    <?php
    $ftp_server="127.0.0.1";//假设Ftp Server的IP是127.0.0.1
    $ftp_user_name="test";
    $ftp_user_pass="test";//假设用户名和密码
    $conn_id = ftp_connect("$ftp_server"); 
    $login_result = ftp_login($conn_id, "$ftp_user_name", "$ftp_user_pass"); 
    if((!$conn_id)||(!$login_result)) { 
            echo "Ftp connection has failed!";
            echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
            die; 
        } else {
            echo "Connected to $ftp_server, for user $ftp_user_name";
        }
    ftp_chdir ($conn_id,"/home");//切换目录到/home
    $destination_file="test001.bin";
    $source_file="c:/command.com";
    //上面这一行应该用form来实现比较好一些
    $upload = ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY); 
    if (!$upload) { 
            echo "Ftp upload has failed!";
        } else {
            echo "Uploaded $source_file to $ftp_server as $destination_file";
        }
    ftp_quit($conn_id); 
    ?>我改了楼上的贴子,加了几行,看看有没有帮助吧。