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.
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
.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.