transferring a remote file to a remote ftp via php - php

I am developing a PHP script.
I would like to transfer a remote ZIP file to a remote FTP (I'm the owner of the FTP account) via PHP.
For example:
I have two hosting accounts: a Hostgator shared account and a Hostgator VPS account.
I want to run my PHP script on the VPS hosting.
I'd like my PHP script to upload this file to the shared hosting account.
Does anyone know a PHP class for this issue?

From the manual:
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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);
?>

Simply:
copy('ftp://user:pass#from.com/file.txt', 'ftp://user:pass#dest.com/file.txt');
The PHP server will consume bandwidth upload and download simultaneously.

Related

php downloading a file from ftp doesn't work locally using XAMPP works only with older files on ftp

When I run the code below the local file gets deleted, I cannot work out why:
$local_file = 'test.csv';
$server_file = 'test_directory/test2.csv';
$ftp_server="servername";
$ftp_user="test";
$ftp_pass="pass";
$conn_id = ftp_connect($ftp_server);
// login with username and password`
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
ftp_pasv($conn_id, TRUE);
// 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);
So I get an error on the console showing cannot open no file or directory.
Edit:
Renaming the file to something else other than I am getting from ftp does not delete the file but still doesn't work.
I am running this in XAMMP htdocs location.
Edit:
Problem is if I add a new file to the ftp server, it works with existing ones even though the permission is the same.
Problem is a corrupt file on the ftp server. I tried with a different file and it works.

Storing files in a subdomain/custom folder using the Storage function in Laravel

I'm trying at the moment to upload files onto a subdomain, hosted on the same server, while the Laravel application is hosted on the main domain. Is there any way to use the Storage functions with the subdomain? Or anything similar?
You can use FTP to transfer file from your server to another server.
First, the receiver must have ftp account.
Second, you can use this below code to transfer your file [source].
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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);
?>

Upload error for PHP file - 500 internal server error?

I have upload script for file uploading from php which is configured to upload file on hosting server from godaddy. Same script I am using on different server domain it is not able to upload on that server which is also godaddy server. I have full access to the folder and server ftp but still didnt find out why this script is not working on that server. Anyone can find out why it is throwing 500 error.
<?php
$ftp_server = "ftp.xxxxx.com";
$ftp_username ="xxxx#xxxx.com";
$ftp_password = "xxxxxxxxxxx";
$destination_folder = "/upload/";
$file_name = $_FILES["userfile"]["name"];
$destination_file = $destination_folder.time().'_'.$file_name;
$file = $_FILES["userfile"]["tmp_name"];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
// upload a file
if (ftp_put($conn_id, $destination_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Do you have shell access? If so, reconfig apache. It may sound not related to your problem, but this will solve it. I had this problem some time ago and rebuilding apache fixed it.

Send files from One server to another.

I have large files (total ~250mb) on my web server. I want to send these files quickly to my 2nd server without using an ftp client. I know the ftp password, username etc of my 2nd server, but how to do it without an ftp client?
$file = 'somehugefile.big';
$remote_file = '/uploads/somehugefile.big';
// 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";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
This example is adapted from the official docs -> http://www.php.net/manual/en/function.ftp-put.php
You will have to change 'upload_max_filesize' and 'post_max_size' in PHP.ini to a higher value as well.
ini_set('upload_max_filesize', '250M');
ini_set('max_execution_time', '999');
ini_set('memory_limit', '500M');
ini_set('post_max_size', '250M');

File won't upload to ftp server

I'm trying to upload a file from my webserver to my game server through a script. The problem is that it can't find the directory.
The full directory is /174.34.132.106 port 27015/tf/addons/sourcemod/configs/tf2items.weapons.txt
This path didn't work so I asked the hosting about it and they insisted that /tf/addons/sourcemod/configs/tf2items.weapons.txt is the correct path but this doesn't work either. The game server is running on a windows server and i'm pretty sure the web server is running on linux. Is my code wrong, do I have to replace the spaces in the directory with %20. Thanks in advance!
$ftp_server="174.34.132.106";
$ftp_user_name="Username";
$ftp_user_pass="Password";
$remote_file = "tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
if(!file_exists($file)) echo "The local file does not exist";
$conn_id = ftp_connect($ftp_server) or die('Unable to create the connection');
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_chdir($conn_id, "174.34.132.106 port 27015/tf/addons/sourcemod/configs/");
echo ftp_pwd($conn_id);
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 noticed that the FTP server you're connecting to is using a non-standard port, so it's probably not making the connection. You need to specify the port in the ftp_connect, like so:
$ftp_server="174.34.132.106";
$ftp_port = "27015";
$ftp_user_name="username";
$ftp_user_pass="password";
$remote_file = "tf/addons/sourcemod/configs/tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die('Unable to create the connection');
The die() will stop the script if it's unable to make the connection. You can add the same after your ftp_login line to make sure that it's actually logging in.
Edit To make sure your file exists, try this above the ftp_put line.
if(!file_exists($file)) echo "The local file does not exist";
Edit 2 After reading down on ftp_put, it says that the remote_file does not support directories. You'll need to use ftp_chdir first.
ftp_chdir($conn_id, "tf/addons/sourcemod/configs/");
Then for remote_file, use just tf2items.weapons.txt. You can still use filepaths for the local file.
If that's a Linux server ensure that you use correct case for directory names.
"tf/addons/sourcemod/configs" is not the same as "TF/addons/sourcemod/configs";

Categories