Is it possible to upload a file into a subfolder on an FTP server?
I send a file daily to an FTP server, scheduled on a cron job that runs a php file. It all works fine, but now I've been asked to change the destination to a subfolder.
Changing *$server_name = 'ftp.website.com'* to *$server_name = 'ftp.website.com/data'*
doesn't seem to work?
I can see the folder exists in filezilla and it has all read/write access in permissions.
I'd be grateful if someone can advise if its a permission issue or whether it's possible at all?
Thanks in advance.
CODE...
//FTP bit...
$server_name = 'ftp.website.com';
$server_username = 'xxxx';
$server_password = 'xxxx';
$conn_id = ftp_connect($server_name);
$login_result = ftp_login($conn_id, $server_username, $server_password);
ftp_pasv($conn_id, true);
ftp_put($conn_id, $zip_filename, $output_dir . $zip_filename, FTP_BINARY);
Seems like you need to use ftp_chdir().
Connect as normal, and then change directories using ftp_chdir().
I assume you are using the ftp functions of PHP. I see two options:
1) Use ftp_put and specify the absolute path on the ftp server. http://php.net/manual/de/function.ftp-put.php
2) Before uploading your file change the working directory on the ftp server using ftp_chdir http://php.net/manual/de/function.ftp-chdir.php
If you use ftp_put you simply specify the absolute destination path on the server
http://php.net/manual/en/function.ftp-put.php
Related
I need to transfer an file created but I keep getting the following error:
Warning: ftp_put(): Can't open data connection for transfer of "/server.txt"
I can access the ftp via an ftp application and I can create and transfer files via the application but when I try to do the same via PHP it doesn't work.
This is my code.
$serverFile = "server.txt";
$localfile = "/var/sites/store/publish/store-201908-1323-rev9286/www/text.txt";
$localfile = "text.txt";
//Connect to ftp
$ftp_server = "111.111.111.11";
$ftp_user_name = "username";
$ftp_user_pass = "password";
/*set up basic ssl connection*/
$ftp = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp, true);
if($login_result){
if (ftp_put($ftp, $serverFile, $localfile, FTP_ASCII))
{
Mage::log("successfully uploaded \n",null,"ftp.log");
}
}
ftp_close($ftp);
Am I missing something here, I do not have access to change anything on the ftp-server but I am aware that we have remotely placed files in the server previously.
Here is also a screenshot of the Ftp-connection I am using in WinSCP. I've tried adding the port. I also tried creating a file on the server and did a ftp_get but got the same result.
I checked your code, and server.txt file will be root of path.
In general, ftp user do not have write permission on root.
Please create new directory with 777 permission and try to upload.
First of all, I know that this a duplicate topic, but the other post that I found were not useful for my situation, so I decided to create a new one.
What I'm trying to accomplish is to get a file from one FTP server and upload it to another FTP server.
I'm using this code:
$ftp_server = "ftp_server";
$ftp_user_name = 'ftp_username' ;
$ftp_user_pass = 'ftp_pass' ;
$localDir = "full/path/";
$serverDir = "full/path/";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $localDir, $serverDir, FTP_BINARY)) {
// ftp_fput($conn_id, $file, $fp, FTP_ASCII))
}
The problem that I have is when you use ftp_put command, it requires a local file, but this file it's not on my computer, so I can't upload it to the other ftp.
Is there a way to upload the file that I just got with ftp_get function into another server using ftp_put? Without the need to download it first on your PC?
Thanks!
Both ftp_get and ftp_put can operate with files only, not folders.
Use ftp_get to download a file from the first server to a local temporary folder/file. And then use ftp_put to upload the temporary file to the second server.
If you want to avoid using a temporary file, you can download the file to memory using ftp_fget and re-upload to the second server using ftp_fput.
PHP: How do I read a .txt file from FTP server into a variable?
Transfer in-memory data to FTP server without using intermediate file
I need to move a backup of my database file to another server automatically using cron. I have some code and it's connecting to the ftp server fine, the problem is it will not move the file. I don't know how to make it show the exact error other than "FTP upload failed!" so I don't know what the real problem is, however, I believe it's got to do with the "real" path of the file on the server and/or the real path of the directory of the other server where it's placing the file.
Any help would be great.
$server = "MyServer";
$ftp_user_name = "MyUsername";
$ftp_user_pass = "MyPassword";
$source = "bumail_data/db-backup-05-09-15__17:23.sql";
$dest = 'db-backup-05-09-15__17:23.sql';
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
ftp_pasv ( $connection, true );
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
The file $source is located in httpdocs/bumail_data/FILE.sql
The folder on the other server I want to put the file in is located before the public_html on a dir named dbbackup. So I guess that is the root dir but not sure.
I hope that helps you understand where the files are and what I maybe need to put in the script. I have tried everything I can think of from ../dir/file to var/host/root/dir/file and so on. It just will not move the file.
I am trying to get some PHP ftp commands to work through my website. All are working, so far, except for PHP_RENAME. I can create folders, add files, delete files, delete folders... but PHP_RENAME fails.
This is the code that creates the folder:
$dir = "/web/proofs/";
$old_dirfolderfile=$dir."oldfolder";
$conn_id = ftp_connect($FTP_SERVER);
ftp_login($conn_id, $FTP_UID, $FTP_PWD);
$result=ftp_mkdir($conn_id, $old_dirfolderfile);
ftp_close($conn_id);
This code works fine and the folder is created.
I can delete the folder with:
$dir = "/web/proofs/";
$old_dirfolderfile=$dir."oldfolder";
$conn_id = ftp_connect($FTP_SERVER);
ftp_login($conn_id, $FTP_UID, $FTP_PWD);
$result=ftp_delall($conn_id, $old_dirfolderfile);
ftp_close($conn_id);
Again, no problem.
But this code:
$dir = "/web/proofs/";
$old_dirfolderfile=$dir."oldfolder";
$new_dirfolderfile=$dir."newfolder";
$conn_id = ftp_connect($FTP_SERVER);
ftp_login($conn_id, $FTP_UID, $FTP_PWD))
$result=ftp_rename($conn_id, $old_dirfolderfile,$new_diroldfolder);
ftp_close($conn_id);
Fails... doesn't rename the directory, results = false.
I've tried using ftp_chdir to go to $dir (which changes if checking the return) and then using just the folder names, fails also. Tried using getcwd() to get the full path and add to the folder name, fails too.
Not sure where to go with this now.
Any suggestions?
ADDITIONAL INFO:
A bit more info... we can use a third party FTP program or direct FTP connection using Network Neighborhood and manipulate, delete, add the folders.
I have an html5 uploader thanks to the following tutorial:
http://www.profilepicture.co.uk/ajax-file-upload-xmlhttprequest-level-2/
works great.. however I would like to upload the files to a different domain... I thought this would be possible as long as the domain, or more specifically the file on the domain I was uploading too had the follwong header:
header("Access-Control-Allow-Origin: *")
Therefore allowing cross domain sharing...
However the upload is not working, is there anything else I am missing, or is it a case that you can communicate across domains but you can't upload files?
Kind regards to any responders...
J
I believe the best option for cross domain upload is to use ftp upload (of course you need to know ftp access credential such as ftp host, username and password.
If your using php as server side language you can try this little piece of code.
I use this on multiple domains within the same web server.
$conn_id = ftp_connect($server) or die("<span style='color:#FF0000'>Can't connect to ".$server."</span>");
$login_result = ftp_login($conn_id, $username, $password) or die();
$upload = ftp_put($conn_id, $server_path, $file, FTP_BINARY);
if (!$upload) {
echo "Error sending image to ".$server;
}
Hope this can help you.