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');
Related
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))
File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.
I keep getting this warning:
Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!
My script is:
<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******#*********";
$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
throw new Exception('Unable to connect');
}
$loggedInnyambungkeftp =
ftp_login($nyambungkeftp, $ftp_user_name, $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';
echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($nyambungkeftp);
?>
PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.
You almost always need to use the passive mode.
For that call the ftp_pasv after the ftp_login:
ftp_pasv($nyambungkeftp, true);
See my article on FTP connection modes, to understand, why you typically need to use the passive mode.
Try two things:
Try FTP_BINARY instead of FTP_ASCII
Try to use passive mode doc here
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.
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.
I am trying to write a small php function that will upload files to an FTP server and I keep getting the same error but I cannot find any fix by googling the problem, I am hoping you guys can help me here...
The error I get is: Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in .
The file was created at the FTP server but it is zero bytes.
Here is the code:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
I forgot to put FTP in passive mode using:
ftp_pasv($cid, true);