0

I have made an upload form in which users can upload their files to a remote FTP server. After they have uploaded their file; I want to make sure that the users can see the files they have uploaded (to the remote FTP server) on my website. How can I accomplish this? I also want the users to be able to download the file. They are mp3 files.

My php code:

<?php

if ( empty( $_FILES['file'] ) ) {
?>
<html>
  <head>

  </head>
  <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>
<?php
return;
} else {
?>
<html>
  <head>
  </head>
  <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>
<?php
}

//Connectiegegevens
$ftp_server = "myserver";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$source_file = $_FILES['file']['tmp_name'];
$destination_folder = "/public_html/wp/wp-content/plugins/AbonneerProgrammas/Albums";
$destination_file = $destination_folder . "/" . basename($_FILES['file']['name']);
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($conn_id, true); 

// check connectie
if ((!$conn_id) || (!$login_result)) { 
    echo "Het spijt ons, er is momenteel geen connectie met de server.";
    //echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
     //echo "upload is gelukt";
}

// upload het bestand
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) { 
echo "Er is iets fout gegaan, excuses voor het ongemak";
} else {

    // succesbericht
    echo "upload is gelukt";
}
    // close the FTP stream 
    ftp_close($conn_id);
?>

My question: How can I make sure that after the users have uploaded their file to the remote FTP server, they will be able to view their file on my website (on the same page)

enter image description here

Greetings,

Johan

PS. I am working in Wordpress, because of this I am writing my files in a plugin. I am unable to use things like the include() and require() statement. I also name my files, after which it gets turned in to a shortcode. The shortcode is added to the page.

  • How is this different to your previous question? [Displaying recently uploaded images from remote ftp server - PHP](https://stackoverflow.com/q/60503236/850848) – Martin Prikryl Mar 05 '20 at 13:32
  • They are not images. But mp3 files. They need to also be downloaded by the user. And all the files from the directory need to be shown, not just the recent ones –  Mar 05 '20 at 13:34
  • For that you also have the answer already: [List and download clicked file from FTP](https://stackoverflow.com/q/52855025/850848). – Martin Prikryl Mar 05 '20 at 13:46
  • But that code doesnt allow the user to download the file? –  Mar 05 '20 at 13:49
  • And why do you think the question is *"List and **download** clicked file from FTP"*? – Martin Prikryl Mar 05 '20 at 13:50
  • So I have to call my file view.php? Because I cant the plugin doesn't allow me to give it .php file names it is very weird I give it a name, and the name changes to a shortcode. I edited my questions. Take a look for yourself (the original question). Also, I should put this code: header('Content-Type: application/octet-stream'); echo file_get_contents('ftp://username:password@ftp.example.com/path/' . $_GET["file"]); In download.php and this: echo "".htmlspecialchars($file).""; in the file in which the upload takes place? –  Mar 05 '20 at 13:53
  • I did just now. –  Mar 05 '20 at 14:12

0 Answers0