How to get HTTP URL of file uploaded to FTP server - php

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

Related

Upload files with PHP on FTP server (WinSCP works)

I want to upload a file on a FTP Server (I use WinSCP). But I don't know how use ftp_put.
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
ftp_close($conn_id);
I receive the file with a form ($_FILES). In $file, maybe $_FILES['myFile']['name'] but I don't know what to put in $ftp_server and $remote_file. The server http://10.XX.X.XX/myProject/ and my file, in WinSCP are in : /var/www/myProject/
Maybe the hostname is XXXX#xxxxx
It works ! Thanks. The problem was the path, the good is : /myProject/
$ftp_server should be the host name you use, when connecting with WinSCP, like "example.com".
$remote_file should be "/var/www/myProject/" . $_FILES['myFile']['name'].
$file should be $_FILES["myFile"]["tmp_name"].

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);
?>

Moving a file to another server via FTP using PHP

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.

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

FTP file upload using php

I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:
<?php
require_once('ftp.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);
// 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);
?>
ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:
There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv
EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:
$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";
What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server?
Thanks.
First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)
ftp_pasv($conn_id, true); // after ftp_login
Second, you have to change to dir first:
ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);
If you want to know what's really going on, try to get error message with error_get_last() or
$php_errormsg

Categories