change ftp_get download directory - php

I am using the below code to download using ftp
$local_file = $_GET['u'];
$server_file = $_GET['u'];
$ftp_host = '';
$ftp_user_name = '';
$ftp_user_pass = '';
$ftp_conn = ftp_connect($ftp_host) or die("Could not connect to $ftp_host");
$login = ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp_conn,true);
ftp_chdir($ftp_conn, '');
$file_list = ftp_nlist($ftp_conn, ".");
//var_dump($file_list);
//var_dump(ftp_get($conn_id, $local_file, $server_file, FTP_BINARY));
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
am getting successfully written to filename but i cant seem to find the file on my computer. Do you if i can change where the file is being downloaded to.

Pass an absolute path (instead of "myfile.txt" try "/home/user/path/to/yourfile.txt")

Related

download file from remote server by php?

Someone tells me. if it is possible for me to download a specific file from a remote server at my location via php?
I've tried codes and I couldn't
<?php
// define some variables
$local_file = '/httpdocs/teste.pdf';
$server_file = 'teste.pdf';
$ftp_server="209.126.127.143";
$ftp_user_name="username";
$ftp_user_pass="password";
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($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 don't think you can download a folder. Try to include filename in $server_file
ftp_get($conn_id, './teste.pdf', '/httpdocs/teste.pdf', FTP_BINARY)
Also, /httpdocs/ should be relative from your FTP user's home folder

File upload via FTP error

Code:
$file = '/export/clients.xml';
$remote_file = '/clients.xml';
$ftp_server = "ftp://my.address.com";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$conn_id = ftp_connect($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";
}
ftp_close($conn_id);
Now the original file path is: ftp://my#address.com/domains/website.com/public_html/export/clients.xml
How should I write the file path so that it could complete the file transfer? I want the file to be copied into the root directory of my FTP server. But it doesn't work this way.
Problem was FTP server name. I removed the ftp:// part and now it's working.

Not able to change directory through ftp in php

I am trying to change the directory with ftp_chdir and it shows me the error:
no such file or directory
But the directory is available at the server side. What am I doing wrong?
Here is my code:
<?php
#session_start();
error_reporting(0);
// define some variables
$local_file = $_GET['f'];
$server_file = $_GET['f'];
$ftp_user_name='abcd';
$ftp_user_pass='abcd';
$ftp_server='abcd';
// 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);
if (ftp_chdir($conn_id, $_SESSION['SITE_IMG_PATH']."x_directory/")) {
echo 'Directory change';
} else {
echo "Couldn't change directory\n :".ftp_pwd($conn_id);
print_r (error_get_last());
}
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "success";
} else {
echo "No";
}
// close the connection
ftp_close($conn_id);
?>
It shows me the error:
Couldn't change directory
my path is
/home/website/public_html/upload/x_directory

ftp_get() corrupt large file download

I have to download a large file(approx~-9mb) in .gz format from the server using ftp. I have written a function which downloads the file completely and correctly but when i put this code online , the file is downloaded but the file gets corrupt.
Here is my code:
function downloadFile($ftp_server, $username, $password, $server_file, $local_file)
{
// download server file
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $username, $password);
ftp_pasv($ftp_conn, true);
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file.";
// exit;
} else {
echo "Error downloading $server_file.";
}
}
Here is how i call it
ini_set('max_execution_time', 0);
$username = "username";
$password = "password";
$xmlFile = __DIR__ . "/monster.xml.gz";
$local_file = __DIR__ . "/monster.xml";
$ftp_server = "ftp.monster.com";
$server_file = "/US~Partner~Sample Feed.xml.gz";
downloadFile($ftp_server,$username,$password,$server_file,$xmlFile);
The file must be downloaded and the then I will convert it into XML for further processing.
Note: The file is downloading perfect on localhost , this problem occurs as we put it on live server.
GZ file may not only have text in it . So you have to download it with binary mode so that anything containing in it could be download successfully
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
echo "Successfully written to $local_file.";
// exit;
} else {
echo "Error downloading $server_file.";
}

ftp_get will not download image

$fp = 'test.png';
$server_file = "dir/testing.png";
//-- Connection Settings
$ftp_server = 'ftp.net';; // Address of FTP server.
$ftp_user_name ='user'; // Username
$ftp_user_pass = 'password'; // Password
// set up basic connection
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$mode = ftp_pasv($conn_id, TRUE);
$download=ftp_get($conn_id, $fp, $server_file, FTP_BINARY);
// try to download $server_file and save to $local_file
if ( $download) {
echo "Successfully $fp\n";
} else {
echo "There was a problem\n";
}
ftp_close($conn_id);
When I run this it says it is successfully, but will not write or download the file.
If I echo file_get_contents($fp); then it will display the image deconstructed into text [in the Web browser]
In need to download images and video...
Try adding
header('Content-Type: image/png');

Categories