0

I'd like to show a list of the files in a directory in my default/files directory. This will show's all files:

/**
 * Helper function to retrieve and display a list of current uploads.
 */
protected function getCurrentUploadsList() {
  $uploads = \Drupal::entityTypeManager()
    ->getStorage('file')
    ->loadByProperties(['status' => FILE_STATUS_PERMANENT]);

  $output = '<h2>Current Uploads</h2>';
  $output .= '<ul>';
  foreach ($uploads as $file) {
    $output .= '<li>' . $file->getFilename() . '</li>';
  }
  $output .= '</ul>';

  return $output;
}

This is my attempt at showing only for a certain directory:

protected function getCurrentUploadsList()
  {
    $folder_uri = 'public://idv-uploads/';
    $uploads = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $folder_uri]);
    $output = '<h2>Current Uploads in idv-uploads Folder</h2>';
    $output .= '<ul>';
    foreach ($uploads as $file) {
      $output .= '<li>' . $file->getFilename() . '</li>';
    }
    $output .= '</ul>';
    return $output;
  }

What am I missing in the loadByProperties? Thanks!

1
  • The URI of every file would have to equal $folder_uri for that code to work. That is why you get no results.
    – cilefen
    Oct 25, 2023 at 1:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.