文件未在远程目录中生成 (ftp)

The file is not being generated in the remote directory (ftp)

我能够正常连接到 filezilla (ftp)。但是 ftp_fput () 方法中的 return return 是错误的。这意味着您无法上传文件。

这是我的代码:

$server = "host.host.ws";
$user = "username";
$pass = "password";
// call function
$conn = uploadFTP($server, $user, $pass, $local_file, $remote_file);
    
function uploadFTP($server, $username, $password, $local_file, $remote_file) {
    
    $connection = ftp_connect($server);
    if (@ftp_login($connection, $username, $password)){
        
        ftp_pasv($connection, true) or die("Unable switch to passive mode");
                            
        $fp = fopen('php://temp', 'r+');            
        fwrite($fp, 'content within the file');
        rewind($fp);

        // the file is not generated in the remote directory: /App/data/Json/File.json
        if (ftp_fput($connection, '/App/data/Json/File.json', $fp, FTP_ASCII)) 
            echo 'generated file!';

        fclose($fp);
        ftp_close($connection);
        return true;

    } else {
        return false;
    }   

只有在显示消息“生成文件!”时才会生成文件,但实际情况并非如此。

代码没有显示任何明确的错误,我的问题是,为什么这段代码没有在目录中生成文件?你能分享这个问题的解决方案吗?

也许FTP可以访问根目录,您需要提供完整路径/var/www/App/data/Json/File.json

ftp_fput($connection, '/var/www/App/data/Json/File.json', $fp, FTP_ASCII)

错误不可见,因为抑制已打开。 在这里象征性地去掉“@”,使用error_get_last() 来查看错误信息

if (ftp_login($connection, $username, $password)) {
    echo 'Connected!';
    
    
    if (ftp_fput($connection, '/App/data/Json/File.json', $fp, FTP_ASCII)) {
        echo 'generated file!';
    } else {
        print_r(error_get_last());
    }
} else {
    return false;
}

您需要确保在服务器上创建了目录结构/App/data/Json/如果没有,则创建它

我觉得会有用 php ftp make multiple directories