1
votes

I have created a form. In this form you can upload a file. The file has to be uploaded to a remote FTP server. The connection to the FTP remote server works. However it does not upload the file. I do not know how to solve this problem. I get the following message when I want to upload: "FTP upload has failed!". This is a message I programmed to show when the upload does not work. No errors.

My PHP code (based on a previous Stack Overflow question):

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

My HTML code (based on a previous Stack Overflow question):

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

I expect that when I submit the form, the file gets transferred to the following path: /public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums. Hopefully you guys can help me out. I am programming in WordPress using the plugin PHPCodeSnippets.

1
So when you say it doesn't work. Does that mean it gets uploaded to the wrong path? Does it not connect at all? Do you get any errors? How do you know it doesn't work? - Dirk Scholten
I have edited the question. - user11716766
The first thing I would do is make sure that there are actually files be posted to your input script, so at the very top of your script, temporarily add print_r($_FILES); exit(); and see if if there are posted files in this array. - Jamie_D
Hi @Jamie_D. I have done that. It returns the following: Array ( [file] => Array ( [name] => cumulus-cloud.jpg [type] => image/jpeg [tmp_name] => /tmp/phpTMWHTW [error] => 0 [size] => 394249 ) ) - user11716766
The next thing I would do is check the (Apache,Ngnx) error logs. - Jamie_D

1 Answers

0
votes
  1. Your code uses FTP active mode, which will hardly work.

    While you are seemingly switching to the passive mode by calling ftp_pasv, it does not work, as it must be called only after ftp_login.

  2. The second argument of ftp_put is a path to a file, not a folder. So it should be:

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