文件未上传到FTP服务器

时间:2020-02-28 12:20:40

标签: php ftp

我创建了一个表格。您可以通过这种形式上传文件。该文件必须上传到远程FTP服务器。与FTP远程服务器的连接正常。但是,它不会上传文件。我不知道如何解决这个问题。我要上传时收到以下消息:“ FTP上传失败!” 。这是我编写的消息,用于在上传不起作用时显示。没有错误。

我的PHP代码(基于先前的Stack Overflow问题):

<?php
if ( empty( $_FILES['file'] ) ) {
    return;
}
$ftp_server = "ftp.myserver.nl";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
$source_file = $_FILES['file']['tmp_name'];

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream 
ftp_close($conn_id);
?>

我的HTML代码(基于先前的堆栈溢出问题):

<html>
<body>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file"/>
<br>
<input name="submit" type="submit" value="Upload uw album" />
</form>
</body>
</html>

我希望在提交表单时,文件将传输到以下路径:/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums。希望你们能帮助我。我正在使用插件PHPCodeSnippets在WordPress中进行编程。

1 个答案:

答案 0 :(得分:0)

  1. 您的代码使用FTP活动模式,该模式几乎无法工作。

    虽然您似乎通过调用ftp_pasv切换到被动模式,但它不起作用,因为只能在之后 ftp_login调用它。

    < / li>
  2. ftp_put的第二个参数是文件而不是文件夹的路径。所以应该是:

    $destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
    $destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
    ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);