Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTTP URL of file uploaded to FTP server

Tags:

http

url

php

ftp

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...

like image 323
Thiyagu Avatar asked Oct 14 '25 18:10

Thiyagu


2 Answers

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.

like image 140
Martin Prikryl Avatar answered Oct 17 '25 09:10

Martin Prikryl


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); 
like image 31
prabushitha Avatar answered Oct 17 '25 07:10

prabushitha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!