0

I want to display files present in the public folder(sites/default/files). So in the module file when I click on a link it will call to js function, How can I get all file names which are present in the public folder in js file.

2
  • Why don't you just point to that exactly file instead of trying to get all the files present in that folder?
    – DarkteK
    Aug 8, 2016 at 13:06
  • Means, full path to that files.? How can I hyperlink to the path while displaying the file names. I am displaying something like this, $data .= '<div class = "public-file-names" >' ."--". $filenames .'<br></div>'; so how can i give full path in backend.
    – Suraj
    Aug 8, 2016 at 13:16

1 Answer 1

0

It depends where you are storing all your files, to use the base url of your site you need to do this:

$base_url."/sites/default/files/xxx.jpg"

If your files are inside some theme:

$base_url."/".drupal_get_path('theme', 'name_theme')."/images/xxx.png

If you are using files with public://, use this:

$uri = $your_file_name; // Example "public://xxx"
$path= file_create_url($uri);

I hope it works in your case

5
  • Thank you, It works fine. your solution will display the full content of the file in browser, but how can I get the popup asking that save file or open file??
    – Suraj
    Aug 8, 2016 at 14:31
  • No problem, if you are using HTML5 on your site you can use this tag: <a href="/xxx.pdf" download target="_blank">
    – DarkteK
    Aug 8, 2016 at 14:43
  • This will open in new tab, but I want to show the popup before displaying the file content. Just now i have asked question please refer this url: drupal.stackexchange.com/questions/210829/…
    – Suraj
    Aug 8, 2016 at 14:50
  • you can't really accomplish this, you can force the tag "a" to do either one way or another, but not both, because it depends on the plugins and components of the user that has installed in his computer
    – DarkteK
    Aug 8, 2016 at 15:12
  • I thinks that depends upon file extension. Because for .csv files it will show popup, nut for .xml,.txt it will not show the popup. So you know how can show that popup to here also?
    – Suraj
    Aug 8, 2016 at 15:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.