P粉7757237222023-08-29 20:07:59
Pay attention to the documentation of PHP's ftp_pasv
function (emphasis mine):
Also note that you are not checking the status return value of the ftp_pasv
call, so you won't notice whether the call actually succeeded (which it most likely won't). Therefore, your script will attempt to establish an active FTP connection. This does not work in containers (unless started with --network=host
), since containers run in a private network via NAT on the host machine.
Solution: Log in first, then enable passive mode (also always check for error return values; many old functions from the PHP standard library do not throw exceptions but rely on error return values):
if (ftp_login($connection, FTP_USERNAME, FTP_PASSWORD)) { if (ftp_pasv($connection, true) === false) { throw new \Exception("无法启用被动模式") } $stream = fopen('data://text/plain,','r'); ftp_fput($connection, $filename, $stream); }