ftp_get(): Transfer failed - php

This is the code that I am using
<?php
// connect and login to FTP server
$ftp_server = "ftp_server";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
ftp_pasv($ftp_conn, TRUE);
ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 2800);
$ftp_username="ftp_username";
$ftp_userpass="ftp_userpass";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$local_file = "local_file";
$server_file = "server_file";
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
{
echo "Successfully written to ".$local_file;
}
else
{
echo "Error downloading ".$server_file;
}
// close connection
ftp_close($ftp_conn);
I get the same error all the time-
"Warning: ftp_get(): Transfer failed in myfile.php on line 33
Error downloading server_file"
I try to see if the file that I am trying to get is the right one so I used
this code-
$res = ftp_size($ftp_conn, $server_file);
if ($res != -1) {
echo "size of $server_file is $res bytes";
} else {
echo "couldn't get the size";
}
I get the file size so the file is exist. The file is about 11MB so the size of the file not suppose to be an issue.
I added the lines :
ftp_pasv($ftp_conn, TRUE);
ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 2800);
After searching solutions on the web but the results are the same with or without those lines...
Any ideas?
Cheerz

You need to call ftp_pasv() after ftp_login(), not before. Check the return value of the function call to see if it succeeds.

Related

Php ftp_get(/Outbox/CCDATA.TXT) I can connect to the server but I am not sure how to get in the correct directory

I would like to download a file from an ftp site. I can connect to the site using this.
I get this error:
Warning: ftp_get(/Outbox/CCDATA.TXT): failed to open stream: No such file or directory in /var/www/html/dashboard/data/cit_file_download.php on line 16
Warning: ftp_get(): Error opening /Outbox/CCDATA.TXT in /var/www/html/dashboard/data/cit_file_download.php on line 16
Error downloading CCDATA.TXT.
conn_id = ftp_connect($ftp_server) or die("Couldn't connect to
$ftp_server");
// try to login
if (#ftp_login($conn_id, $ftp_username, $ftp_userpass)) {
echo "Connected as $ftp_username#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_username\n";
}
// close the connection
ftp_close($conn_id);
so I know my credentials work. My trouble seems to be in adding a path. the file I need is in a folder called "Outbox" and I have not been successful with anything I have tried.
This is my current code. Thanks for the help
$local_file = "order.txt";
$server_file = 'CCDATA.TXT';
$ftp_username="removed";
$ftp_userpass="removed";
$ftp_path = '/Outbox/';
$ftp_server = "removed.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to
$ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
// download server file
if (ftp_get($ftp_conn, $ftp_path.$server_file, $local_file,
FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
// close connection
ftp_close($ftp_conn);
The local file should come first then the server file and path.
if (ftp_get($ftp_conn, $local_file, $ftp_path.$server_file,FTP_ASCII))

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

how to download a file from windows server using ftp?

I am trying to download a file from windows server to a local ubuntu machine. below is the code i used.
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $username, $password);
$local_file = "test.php";
$server_file = $_SERVER['DOCUMENT_ROOT'] . "/plugins/myplugin/controllers/test.php";
$handle = fopen($local_file, 'w');
if (file_exists($server_file)) {
echo "exist";
} else {
echo "not exist";
}
if ((!$ftp_conn) || (!$login)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}
// download server file
if (ftp_fget($ftp_conn, $handle, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file.";
} else {
echo "Error downloading server file.";
}
// close connection
ftp_close($ftp_conn);
exit;
Always getting Error downloading server file. The error getting is
ftp_get("test.php"): failed to open stream:
See the documentation:
Specifically, the second argument:
handle: An open file pointer in which we store the data.
You are passing it a string, not a file pointer.
See the example code in the manual:
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');

ftp_get can't connect to file/ directory

I'm connecting to FTP server and trying to download a file from it, but I'm getting an error of
Warning: ftp_get() [function.ftp-get]: Can't open /home/a*******/public_html/files/test.txt: No such file or directory in /home/a*******/public_html/MainHomescreen.php on line 213
if(isset($_REQUEST['download']))
{
// connect and login to FTP server
$ftp_server = "********.*****.***";
$ftp_username = "a*******";
$ftp_userpass = "******";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $selected)
{
//echo $selected;
$local_file = "local.zip";
$server_file = "/home/a*******/public_html/files/$selected";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
}
}
// close connection
ftp_close($ftp_conn);
}
it's connecting to the server ok as I've checked that but when I try and download a file it comes up with the error above, but does echo the error line too. I'm not sure whether I've connected to it wrong in $local_file and $server_file, which is my best bet as to where I've gone wrong. Hoping someone might be able to help. Thanks.

ftp_get giving error while fetching a file

Im trying to fetch a csv file from a remote server with ftp_get
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, TRUE);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$okk=0;
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) (line 31)
{
$okk=1;
}
but its giving following error
Warning: ftp_get(): Opening BINARY mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31
I tried changing it to ascii mode then too it gave error
Warning: ftp_get(): Opening ASCII mode data connection for /abc/abc.csv(198528 bytes). in /home/a/b/c/cm_data/d.php on line 31
i also tried using ftp_pasv($conn_id, TRUE); too but still gives error.
What is the problem please help!!
ftp_pasv needs to be called after ftp_login!
You seem not to treat error cases from ftp_connect and ftp_login.
Please try the following code and see if it gives some errors:
<?php
$ftp_server = $ftp_server;
$ftp_user = $ftp_user_name;
$ftp_pass = $ftp_user_pass;
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// try to login
if (#ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected as $ftp_user#$ftp_server\n";
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
$okk=1;
}
} else {
echo "Couldn't connect as $ftp_user\n";
}
// close the connection
ftp_close($conn_id);
?>

Categories