download file from remote server by php? - 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

Related

change ftp_get download directory

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")

How to upload file on server using php

Im using php code to upload a file.
The problem is i can only code it to upload file on my local host.
How do I code the target directory that can upload file in server using info such as localhost, name, password etc.
You are looking for a remote file transfer such as ftp.
Taken from the PHP manual:
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// 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_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>

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 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