ftp_connect error (download file from FTP) - php

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

Related

Copy a file from FTP server to Cpanel using Php

Can you please assist me. I am trying to copy a file to the folder inside the application. The script is working on my local machine however when I run it on Cpanel server it shows an error "ftp_login() expects parameter 1 to be resource, Boolean given"
Here is the script which I tested
$folder_path = "192.xx.xx.xx\TMS";
$local_file = "CurrentFile\Inbound.xls";
$server_file = "CurrentFile\Inbound.xls";
//-- Connection Settings
$ftp_server = "192.xx.xx.xx"; // Address of FTP server.
$ftp_user_name = "FTP server username"; // Username
$ftp_user_pass = "FTP server Password"; // Password
$target = 'CurrentFile';
if (!file_exists($target))
{
die("Target directory doesn't exist.");
}
else if (!is_writable($target))
{
die("Insufficient privileges to write to the target directory!");
}
// 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";
}
function fileExists($path)
{
return (#fopen($path,"r")==true);
}
ftp_close($conn_id);
I think the script is failing to connect to the FTP server.
If I add this just after I create the connection It returns "connection Failed".
if(!$conn_id) {
die("Connection failed!");
}
I am not used this but I have suggested go to this tutorial ftp_login expects parameter 1 to be a resource and let me know what happen to this

Uploading local to server using php and ftp access

I am trying to upload my local file to server with the following code copied from stackoverflow. I couldnt map the files right and looking for assistance.
I got two test cases with $localfile.
mapping with relative path using c:, it throws filename prohibited.
mapping with server path. it says connected and written successfully. But in server the file size is 0KB only.
The code:
//$local_file = 'C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv';
//Warning: ftp_put(): Prohibited file name: C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\sample.csv in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\projects\alaks\filestoanotherserver\test.php on line 28
// The filename that i got the php code is test.ph
$local_file = 'sample.csv';
//Connected!Successfully written to sample.csv - but not written.
//The file size is showing 0KB.
$server_file = 'http://iseedtechnologies.in/sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>
Working code (Removed the http:// and it worked)
$local_file = 'sample.csv';
$server_file = 'sample.csv';
// set up basic connection
$conn_id = ftp_connect("iseedtechnologies.in");
// login with username and password
$login_result = ftp_login($conn_id, '****', '****');
echo is_array(ftp_nlist($conn_id, ".")) ? 'Connected!' : 'not Connected! :(';
ftp_pasv($conn_id, true);
// try to download $server_file and save to $local_file
if (ftp_put($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);
?>

Creating a CSV file on a remote FTP server [duplicate]

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';
}

Unable to send file from ftp to another ftp

I have searched a lot for the answer and found some results but none of one is working for me. I want to transfer zip file from one server to another server using ftp functions. File went transferred but without data. Here is my code as follows:
<?php
$file = 'LICENSE.zip';
$fp = 'LICENSE.zip';
$ftp_server = "ftpserver name";
$ftp_user_name = "ftp username";
$ftp_user_pass = "ftp password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// login with username and password
// upload a file
ftp_chdir($conn_id,'/dev');
if (ftp_put($conn_id, $file, $fp, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
fclose($fp);
?>
Even I am unable to transfer a simple .php or .txt file with their data. In this case only files without data is moving.

cronjob to copy a file to our server via FTP

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

Categories