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);
?>
Related
I do not know what to do anymore I'm trying to overwrite a file from ftp with windows to a remote linux server using php.
I already tried with ftp_pasv in true, saying that I can not change the mode, I put this line in my file vsftpd.conf: pasv_promiscuous = YES and it does not work either.
Check the route in filezilla and it's the same.
Code php:
$ftp_server = 'ip';
$ftp_user_name = 'user';
$ftp_user_pass = 'pass';
$file = 'C:/archivos/sip_trunk.conf';
$remote_file = '/home/sk/sip_trunk.conf';
$conn_id = ftp_connect($ftp_server) or die("Unable to connect to host");
ftp_pasv($conn_id, true) or die("Unable switch to passive mode");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Authorization failed");
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
$text = "Upload $file\n";
} else {
$text = "Error Upload $file\n";
}
ftp_close($conn_id);
Error ftp_pasv in false:
ftp_put(): Could not create file.
Arguments:
FTP Buffer resource #315
"/home/sk/sip_trunk.conf"
"C:/archivos/sip_trunk.conf"
1
Error ftp_pasv in true:
Unable switch to passive mode
I do not think it's because I'm running the code on a larvel controller
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))
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 was working on with a FTP upload from my php page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
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);
?>
It gives me a Warning in browser like
Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.
I checked the access permissions on the file. But its accessible. Can any one tell me why this happens?
Most probably this is a permission issue. When you are uploading a file via FTP, you also need to check the directory's permission. When you're saying it's accessible, it doesn't mean it's writable.
You don't check the result of the login operation:
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
You should also try a manual FTP operation from the PHP host to the FTP host to ensure that you can log-on with these credentials and put a file. This will help you establish whether it's your code at fault or the FTP credentials.
<?php
class Ftp {
function upload()
{
$ftp_server="50.56.113.39";
$ftp_user_name="******";
$ftp_user_pass="***";
$file = "form.pdf";//tobe uploaded
$remote_file = "uploads1/test.pdf";
// 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_BINARY))
{
echo "successfully uploaded $file\n";
//exit;
}
else
{
echo "There was a problem while uploading $file\n";
//exit;
}
// close the connection
ftp_close($conn_id);
}
}
?>