文件未上载到FTP服务器



我创建了一个表单。在这个表单中,你可以上传一个文件。必须将文件上载到远程FTP服务器。与FTP远程服务器的连接正常。但是它不会上传文件。我不知道如何解决这个问题。当我想上传时,我会收到以下消息:"FTP上传失败!">。这是我编程的一条消息,用于在上传不起作用时显示。没有错误。

我的PHP代码(基于之前的堆栈溢出问题(:

<?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. 您的代码使用FTP活动模式,这将很难工作。

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

  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); 
    

最新更新