Variation of my question on Ubuntu.SE:
This is (basically) what I'm doing when I log into a FTP:
ftp user:password#server
ftp: user:password#server: Unknown host
ftp> echo HELLO WORLD!
ftp> quit
Is it possible to "echo" over ftp in PHP?
<?php
$ftp_server = "server";
$ftp_user_name = "user";
$ftp_user_pass = "SuperSecretPassword";
$message = "Hello World!";
// connect
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Echo Message
$upload = ftp_echo($conn_id, $message);
// close the FTP stream
ftp_close($conn_id);
?>
Maybe I'm an idiot, but all the commands I see are for pushing, pulling or doing stuff locally. Does something else act as 'ftp> echo "Hello World!"' and am I'm looking right at it without realizing it?
I think you want ftp_raw. You'd use this to put an arbitrary command to your ftp server.
<?php
$fp = ftp_connect("ftp.example.com");
/* This is the same as:
ftp_login($fp, "joeblow", "secret"); */
ftp_raw($fp, "USER joeblow");
ftp_raw($fp, "PASS secret");
?>
Related
I'm trying to download file via php script, but unfortunately with this script i get error:
PHP Warning: ftp_connect(): php_network_getaddresses: getaddrinfo
failed: nodename nor servname provided, or not known in
/Users/apple/projects/asystem/download_dump.php on line 15
i try to put different FTP address test it and it worked fine
so the problem is 100% in the server address...
// define some variables
$folder_path = "/Users/apple/projects/asystem";
$local_file = "auct_lots_full.xml.zip";
$server_file = "auct_lots_full.xml.zip";
//-- Connection Settings
$ftp_server = "ftp://xxxx_user:Eecohshxxxxxx#auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
#$destination_file = "FILEPATH";
// 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);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
so yes! the problem was that i use wrong path to the FTP. This code works:
//-- Connection Settings
$ftp_server = "auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
This question already has answers here:
Creating and uploading a file in PHP to an FTP server without saving locally
(4 answers)
Closed 7 years ago.
I want to create a CSV file in a remote server in a certain folder. I have the FTP details, and the code for creating the CSV ready, but my problem is I am unable to save that CSV in that remote server. I used below code to connect to FTP:
$ftp_server = "127.0.0.1"; // FTP Server Address (exlucde ftp://)
$ftp_user_name = "username"; // FTP Server Username
$ftp_user_pass = "password"; // Password
// Connect to FTP Server
$conn_id = ftp_connect($ftp_server);
// Login to FTP Server
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
Now I want to save the file to the connection made above.
Currently below is the code that i used to open a csv stream.
$structure = './orderexport/'.$order_date;
if (!mkdir($structure, 0777, true)) {
$filename = "orderexport/".$order_date."/test_".$order_id.".csv";
} else{
$filename = "orderexport/".$order_date."/test_".$order_id.".csv";
}
$fp = fopen($filename, 'w');
Is there anything I need to do on csv end too to save it to remote server?
Just for your reference ( Code not tested , just make use of ftp_mkdir() & ftp_put() )
$ftp_server = "127.0.0.1"; // FTP Server Address (exlucde ftp://)
$ftp_user_name = "username"; // FTP Server Username
$ftp_user_pass = "password"; // Password
// Connect to FTP Server
$conn_id = ftp_connect($ftp_server);
// Login to FTP Server
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$structure = './orderexport/'.$order_date;
if (ftp_mkdir($ftp_conn, $structure )) {
$filename = "orderexport/".$order_date."/test_".$order_id.".csv";
ftp_put($conn_id, $filename, $file, FTP_ASCII);
echo 'Success';
}
else{
echo 'Error';
}
Is it possible for a cron job to copy a file from a remote server and move it to our server and over-right the file currently sitting there?
I have looked for an answer on here and not found a suitable solution as most are moving a file to and not from a remote server
there will be two sets of ftp details to include.
This is for a product feed and i really cant get my head around it.
Am i on the right line of thinking with this that i have adapted.
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Yes, it's possible.
Cronjobs are made to execute scripts periodically (every hours, every day, etc...).
So everything you can do with a script, you can do with cronjobs.
after playing around with the code i noticed i was using ftp_put and not ftp_get
here is the working code
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
if (ftp_get($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
I'm trying to get a directory listing from a FTP server which only accepts FTP connections over explicit TLS.
<?php
$ftp_server = "...";
$ftp_user_name = "...";
$ftp_user_pass = "...";
// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
echo $login_result . "<br/>";
// change dir to "data"
$chdir_result = ftp_chdir($conn_id, "data");
echo $chdir_result . "<br/>";
echo ftp_pwd($conn_id) . "<br/>";
// get contents of the current directory
$contents = ftp_rawlist($conn_id, ".");
// output $contents
var_dump($contents);
// close the ssl connection
ftp_close($conn_id);
?>
The script hangs for about a minute and gives the following output:
1
1
/data
bool(false)
I also tried using ftp_nlist and print_r with no avail. (print_r just prints blank).
If you have a firewall or NAT enabled on your server then you have to use passive mode for your ftp connection
Add this before ftp_rawlist()
ftp_pasv( $conn_id, true );
I'm trying to connect to my server using php script to upload some files...
But it doesn't connect...
I dont know what is the error...
I'm sure that ftp is enable, i checked it through php_info()
What may be the error...
<?php
error_reporting(E_ALL);
$ftp_server = "server.com"; //address of ftp server (leave out ftp://)
$ftp_user_name = "Username"; // Username
$ftp_user_pass = "Password"; // Password
$conn_id = ftp_connect($ftp_server); // set up basic connection
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
echo "Connected as ,$ftp_user_name,$ftp_user_pass \n";
} else {
echo "Couldn't connect \n";
}
.....
.....
....
....
ftp_close($conn_id); // close the FTP stream
?>
maybe you have to turn on the passive mode by doing:
ftp_pasv($conn_id, true);
directly after your ftp_login
PS: why do you do a double login? write
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result) {
instead of
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
This looks wrong to me:
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass)) {
You should just need:
$login_result = ftp_login($conn_id,$ftp_user_name,$ftp_user_pass);
if ($login_result) {
Otherwise it will attempt to log in twice, this could be the issue.
Also try adding or die to the ftp_conect to see if it can even connect to server.
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
Check the server you're trying to connect actually accepts connections from wherever this script is running from by using a regular FTP client. Your code may be correct but the FTP server isn't accepting connections from your server, or there's a firewall blocking things.
Most PHP functions will log error information internally that you can retrieve with error_get_last() and/or $php_errormsg. Some diagnostic information as to why the login call is failing may be stored in there.