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.
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.
Below is my code to upload file to other server ,
$ftp_server="host";
$ftp_user_name="";
$ftp_user_pass="";
$file = "referesh.php";//tobe uploaded
$name = "diff_adtaggeneration";
$remote_file = $name."/referesh.php";
// 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);
$res = ftp_rawlist($conn_id, $name);
//print_r($_SERVER);exit;
if(count($res) > 0)
{
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
else
{
ftp_mkdir($conn_id, $name);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
ftp_close($conn_id);
above code works perfectly, the file succesfully uploaded to other server, the new folder name 'diff_adtaggeneration' will create in root of the directory and file uploaded in that folder, and I need to get the uploaded file path ..! I use print_r($_SERVER) , I know this shows only current server details, but how to get the root directory full file path of uploaded server(other server). Any help appreciated...
There's no magical way to find out an HTTP URL of the file uploaded to an FTP server, that happens to share a file storage with the HTTP server.
It's a matter of configuration of both the FTP and HTTP servers; how do they map virtual HTTP and FTP paths to and from an actual file system paths.
The mapping can be completely virtual and undeterministic.
Having that said, most web hostings have some root both on FTP and HTTP, from where the mapping is deteministics.
If you connect to the FTP, you usually land in the root folder of the HTTP hosting, or immediately below it (there would be a subfolder like httpdocs or www that maps to the HTTP root).
If the FTP account is chrooted, the root folder will be / (or /httpdocs, etc).
If you have your own domain (www.example.com), then an FTP path like /index.html maps to https://www.example.com/index.html.
That's the most simple case. It can be a way more complicated. But that's something we cannot help you with. That's a question for your web hosting provider and/or administrator.
Using ftp_pwd you can achieve to this kind of a level. But will be relative to (may be public_html). If you want to access the file using http add the file to a http exposed directory.
$ftp_server="host";
$ftp_user_name="";
$ftp_user_pass="";
$file = "referesh.php";//tobe uploaded
$name = "diff_adtaggeneration";
$remote_file = $name."/referesh.php";
// 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);
$res = ftp_rawlist($conn_id, $name);
//print_r($_SERVER);exit;
if(count($res) > 0)
{
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
else
{
ftp_mkdir($conn_id, $name);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
$remotePath = ftp_pwd($conn_id)."/".$remote_file; ///public_html/name/refresh.php
ftp_close($conn_id);
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.
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";
how can i do extract zip file for script setup in safe_mode on
i'm trying this
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if (($v_result_list = $archive->extract()) == 0) {
die("Error : ".$archive->errorInfo(true));
}
echo "<pre>";
var_dump($v_result_list);
echo "</pre>";
but i get OWNER error on the extracted dir
How do i fix this problem, or i think re connect to this ftp again and upload and extract file to this ftp
$local_file = './arcive.zip';
$ftp_path = '/extract';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
How do you think i can do?
when a file is unzipped it preserves the owner and permissions information. In your case it's likely that the permissions do not allow world access. if the owner of the file is different from the account that PHP runs under, you cannot chown the file or change permissions. Unless PHP is run as root (and who does that?). in safe mode you can't even do that as root.
Ask the person who archives the file to make it "read & write" for everybody.
As for the second part of the question, you cannot extract something on another server over ftp protocol. you'd have to ssh into the server.
http://phpseclib.sourceforge.net might be of help in that