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)
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 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);
?>
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 need to write a PHP script that downloads a file (master.csv) from an ftp server (ftp.example.com) and prepares it for processing and uploading. I've tried this a number of times but my code is no good, I'm new to PHP. How would I go about doing this?
<?php
///vars
$local_file = 'xmitpart2.csv';
$server_file = 'master.csv';
$ftp_server="ftp.example.com";
$ftp_user="username";
$ftp_pass="paswd";
//connect to server
$conn_id = ftp_connect($ftp_server);
// login to ftp
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
//download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII))
{
echo "Holy Crap Finally!\n";
}
else
{
echo "Of Course.\n";
//WRONGO DIE DIE DIE
die;
}
// close the connection
ftp_close($conn_id);
You can use ftp_get();
It allows you to get a file from an FTP server and save it locally.
Once you have it saved locally you can do what ever you want to "prepare it" as you say.
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);
?>