Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to properly print all the files name in a directory using php

The code below successfully print an array result of all the php files in a folder.

$search_data = glob('download/*.php');
print_r($search_data);

Below is the Array Result

Array ( [0] => download/index.php
[1] => download/register.php ) 

Here is my question: Please How do I print all the files.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried adding the code below but it throws error
Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given

$json = json_decode($search_data, true);
foreach ($json as $data) {

//print or list all the files 
    echo  $files = $data;
  echo "<br>";
}

>Solution :

$search_data isn’t a json string to decode. It’s already an array. You’d just loop through it normally. Then use echo "$data<br>\n";

Thus you’d have:

$search_data = glob('download/*.php');
foreach ($search_data as $data) {
    echo "$data<br>\n";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading