How to get HTTP URL of file uploaded to FTP server
Solution 1
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.
Solution 2
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);
Thiyagu
Updated on June 04, 2022Comments
-
Thiyagu almost 2 years
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...-
Lawrence Cherone over 6 yearsYour need to do somthing like
'/var/www/html/'.dirname($remote_file)
as you cant get absolute path from ftp. -
drew010 over 6 yearsCheck out ftp_pwd but it'll possibly be relative to the FTP root. The server may or may not expose the real, full path to the remote directory in which case you'll just need to know it.
-
Thiyagu over 6 yearsIf file is upload to that server , I need to run that upload file on that server
-
Thiyagu over 6 yearsIs there possible to run uploaded file?
-
Martin Prikryl over 6 yearsWhat do you mean by "run"? That's too vague. Do you want to open the file in browser (=redirect browser to that server and script)?
-
Thiyagu over 6 years@martin:yes,need to redirect browser to that server and script
-