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.
Related
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
<?php
$local_file = 'filename';
$remote_file = '/folder name/filename';
$ftp_server ='IP';
$ftp_user_name = 'NAME';
$ftp_user_pass = 'PW';
// 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);
// turn passive mode on
ftp_pasv($conn_id, true);
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
// close the connection
ftp_close($conn_id);
?>
I'm using the above script successfully on a different server with file located in the root folder. But on this one I need to fetch a file that's in a sub-directory (let's say 'folder name' for now). I've tried different things but all throw an error "failed to open stream: No such file or directory".
I've tried:
$remote_file = '/folder name/filename.csv';
$remote_file = './folder name/filename.csv';
$remote_file = '../folder name/filename.csv';
$remote_file = '/../folder name/filename.csv';
It's my first time dealing with ftp_get command. Can someone please help I'm badly stuck... don't know what I'm doing wrong here
For those who might run into the same issue, I've sorted it out by added the following lines of code (using the function ftp_chdir) and it worked fine:
// turn passive mode on
ftp_pasv($conn_id, true);
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// try to change the directory to somedir
if (ftp_chdir($conn_id, "Live Stock")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
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);
?>
I am having a php enabled server1. I have a php code for file upload. But i need the file to be saved on server2.
I have FTP access to server2.
While searching i found this code,
<?php
$ftp_server = "199.53.23.1";
$ftp_user_name = "xxxx";
$ftp_user_pass = "**********";
$remote_dir = "http://server2/Images/";
// 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);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
$file = $_FILES["uploadedfile"]["tmp_name"];
$remote_file = $_FILES["uploadedfile"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
?>
It said unable to connect to Server.
Anyone have an idea on this type of requirement?
Please help.
UPDATE
Server2 doesn't support PHP
Here your line
$login_result = #ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
Remove #
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
For SFTP
$conn_id = ssh2_connect($ftp_server, 22);
ssh2_auth_password($conn_id, $ftp_user_name, $ftp_user_pass);
$sftp = ssh2_sftp($conn_id);
Uses example: $stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
*send a file
ssh2_scp_send($conn_id, '/local/filename', '/remote/filename', 0644);
*fetch file
ssh2_scp_recv($conn_id, '/remote/filename', '/local/filename');
*Create a new folder
ssh2_sftp_mkdir($sftp, '/home/username/newdir');
*Rename the folder
ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');
*Remove the new folder
ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');
*Create a symbolic link
ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');
*Remove a file
ssh2_sftp_unlink($sftp, '/home/username/myfile');
I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it