You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP通过ftp_put上传文件后,如何获取服务器路径及文件访问URL?

How to Get Server File Path (with Username) and Access URL After FTP Upload in PHP

Let’s work through this together—since the FTP protocol doesn’t natively return full server filesystem paths or HTTP URLs after an upload, you’ll need to pair your FTP upload logic with some pre-configured server/domain details. Here’s how to do it step by step:

1. Get the Full Server File Path (e.g., /home/username/public_html/website/file.php)

First, you need to know the server-side absolute path that maps to your FTP root directory. Most shared hosts set your FTP root to something like /home/username/public_html—you can find this info in your hosting control panel (cPanel, Plesk, etc.), or by creating a quick PHP script on your server that echoes $_SERVER['DOCUMENT_ROOT'].

Once you have that root path, combine it with the remote path you used in ftp_put() to get the full server path:

// Pre-configured server root (from your hosting account details)
$serverRoot = '/home/username/public_html';

// Remote path you passed to ftp_put()
$remoteFtpPath = '/website/file.php';

// Build the full server file path
$fullServerPath = rtrim($serverRoot, '/') . '/' . ltrim($remoteFtpPath, '/');

// Output: /home/username/public_html/website/file.php
echo $fullServerPath;

Key Notes:

  • If you used a relative path in ftp_put() (e.g., website/file.php instead of /website/file.php), use ftp_pwd() to get your current FTP working directory first, then append the relative path to normalize it to an absolute path.
  • The rtrim() and ltrim() calls avoid duplicate slashes (like /home/username/public_html//website/file.php) which can cause issues.

2. Get the Access URL (e.g., http://example.com/website/file.php)

You’ll need to know the domain (or subdomain) linked to your web root (usually the same directory as $serverRoot). Combine that domain with the remote path (relative to your web root) to form the URL:

// Pre-configured website domain (use https:// if your site is secured)
$websiteDomain = 'http://example.com';

// Remote path relative to your web root (same as the path in ftp_put())
$remoteWebPath = '/website/file.php';

// Build the full access URL
$accessUrl = rtrim($websiteDomain, '/') . '/' . ltrim($remoteWebPath, '/');

// Output: http://example.com/website/file.php
echo $accessUrl;

Key Notes:

  • Always use https:// if your site uses SSL/TLS to avoid mixed-content errors.
  • If you uploaded the file to a subdirectory that’s not directly under your web root, adjust $remoteWebPath to match your site’s URL structure.

Why This Approach Works

FTP operates separately from your web server’s file system and HTTP setup—so it can’t automatically return these details. By pre-configuring these static root paths and domains (which don’t change for your hosting environment), you can reliably build the exact paths and URLs you need right after a successful ftp_put() call.

内容的提问来源于stack exchange,提问作者Smart Cms

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:56:05