Rezoo Aftib
Rezoo Aftib

Reputation: 73

Browser will not display new files - PHP

I was busy fixing an error in which dots were being shown everytime I displayed a file. After I fixed the error, the dots were not shown, and it displayd the first file I uploaded. However, when I want to upload a new file, it does not show. The old file just remains there (two files should be shown).

I used Continue to fix the dots showing.

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 = "myuser";
$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 {

// weergeef het bestand & download
$contents = ftp_nlist($conn_id, $destination_folder);

   foreach ($contents as $mp3_url) { 
    $filename = basename($mp3_url, ".mp3");
// Dit zorgt ervoor dat de punten niet te zien zijn
    if($filename == "." && "..") {
    continue;
    print_r($filename);
   }


    }
?>

<table>
  <tr>
    <th>Programma</th>
  </tr>
  <tr>
     <td><?php echo "<a href='$mp3_url'>$filename</a>"; ?></td>
   </tr>
</table>


<?php
    // Doe dit aan als er ergens een error is: var_dump(error_get_last($contents));

    // succesbericht
    echo "upload is gelukt";
}

    // Doe de FTP connectie uit
    ftp_close($conn_id);

?>

Only one file is shown while two are uploaded

Upvotes: 0

Views: 52

Answers (1)

Felan
Felan

Reputation: 93

You're using a foreach loop to get the data:

foreach ($contents as $mp3_url) { 
    $filename = basename($mp3_url, ".mp3");
// Dit zorgt ervoor dat de punten niet te zien zijn
    if($filename == "." && "..") {
    continue;
    print_r($filename);
   }

But then you're only displaying the result of the last iteration of that loop:

   <tr>
     <td><?php echo "<a href='$mp3_url'>$filename</a>"; ?></td>
   </tr>

Try:

$filenames = [];
foreach ($contents as $mp3_url) { 
    // {...} your remaining code
    $filenames[] = $mp3_url;
}

And then

foreach ($filenames as $filename) {
    echo $filename;
}

Storing those filenames in an array as opposed to a string will make you able to iterate over them.

Upvotes: 1

Related Questions