使用PHP通过FTP上传文件

时间:2014-08-13 04:23:45

标签: php sql csv ftp

我很好奇如何使用PHP通过FTP上传文件。假设我有SQL函数,它将返回文件名(动态名称)并使用文件名返回上传文件。我怎么能这样做?

我之前尝试过并尝试使用命令提示符。但错误是" PHP警告:ftp_put:无法打开流:没有这样的文件或目录......"

我的代码:

<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");

/$query = pg_query($db, "select lookup_cell();");

$arr = pg_fetch_array($query, 0, PGSQL_NUM);

$file = $arr[0].'.csv'; 
$remote_file = '/nury/'; // <-- my directory on server 

$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// Passive mode
// ftp_pasv ($conn_id, true);

$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);

//var_dump($ftp); die();

// upload a file
if ($ftp) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

?>

1 个答案:

答案 0 :(得分:-1)

删除被动模式的注释,然后尝试一次它将起作用。

 <?php
    $db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");

    /$query = pg_query($db, "select lookup_cell();");

    $arr = pg_fetch_array($query, 0, PGSQL_NUM);

    $file = $arr[0].'.csv'; 
    $remote_file = '/nury/'; // <-- my directory on server 

    $ftp_server = '192.168.1.128';
    $ftp_user_name = 'polban';
    $ftp_user_pass = 'polban2014';

    // set up basic connection
    $conn_id = ftp_connect($ftp_server);

    // login with username and password
    $login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

    // Passive mode
    ftp_pasv ($conn_id, true);

    $ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);

    //var_dump($ftp); die();

    // upload a file
    if ($ftp) {
     echo "successfully uploaded $file\n";
    } else {
     echo "There was a problem while uploading $file\n";
    }

    // close the connection
    ftp_close($conn_id);

?>