0
votes

I'm curious how to upload file through FTP using PHP. Let's say I have SQL function and it will return a filename (dynamic name) and upload file using the return of filename. how can i do this?

i have try before and try in command prompt. but the error is "PHP Warning: ftp_put : failed to open stream: no such file or directory in ...."

my code :

<?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
Is the /nury/ in your home directory? Assuming you're using unix, cd to your /nury/ directory and run pwd. - Scopey
I am not getting your purpose to use FTP in php......why not ou make one simple php script which can allow to upload php file via simple Uploader using move_uploaded_file function I have seen many readymade scripts which are giving you facility to upload file , change file contents also you can create diractory - Chintan Gor

1 Answers

-1
votes

Remove the comment for Passive mode and then try once it will work.

 <?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);

?>