How to get HTTP URL of file uploaded to FTP server

2019-01-27 03:31发布

问题:

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

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



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


标签: php http url ftp