PHP ftp_connect()不工作并返回false

cetgtptt  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(115)
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $ftpServer = 'ftp://123.website.com';
    $ftpUsername = 'username';
    $ftpPassword = 'password';
    $remotePath = $_POST['remotePath'];

    $fileToUpload = $_FILES['fileToUpload']['tmp_name'];
    $fileExtension = $_FILES['fileToUpload']['type'];
    $fileName = $_POST['file_name'] . '.' . $fileExtension;
    
    $conn = ftp_connect($ftpServer, 80);
    if ($conn) {
        $login = ftp_login($conn, $ftpUsername, $ftpPassword);
        if ($login) {
            if (ftp_put($conn, $remotePath . $fileName, $fileToUpload, FTP_BINARY)) {
                echo "<p>File upload succesfull</p>";
            } else {
                echo "<p>Error when uploading file</p>";
            }
            ftp_close($conn);
        } else {
            echo "<p>FTP-Login failed</p>";
        }
    } else {
        echo "<p>Couldnt connect to FTP server</p>";
    }
}
?>

我得到
无法连接到FTP服务器
港口是80。我可以用正确的登录信息打开FTP服务器。是什么让我无法与之连接?

yc0p9oo0

yc0p9oo01#

ftp_connecthostname参数采用 hostname(或IP地址),而不是URL。
所以应该是:

$ftpServer = '123.website.com';
...
$conn = ftp_connect($ftpServer, 80);

相关问题