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.
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))
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');
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 am using this code:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$local_file = "local.zip";
$server_file = "server.zip";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
// close connection
ftp_close($ftp_conn);
?>
In order to get a specific file from a server.
I tried also using -
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
insted of
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
I kepp getting the error-
Warning: ftp_get(): RETR in myfile.php on line 13.
Please Help.
Shimon, when you send and ftp_get, you're sending an RETR command to the ftp server requesting the desired file. It seems that RETR is disabled on your FTP server. Try to get your file with your favorite FTP client to see if will work an tell us the result here. (Also, check if you have read permissions on that folder)
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);