1

How do I use Linux commands in PHP code to fetch all files names and their size and path of files from all directories and subdirectories.

I am running a scan code. where I need to fetch the files and perform a scan. The issue is that when I run on linux server it takes too long to perform the scan. So, i thought of adding linux commands to my php script and check that if server is linux then instead of my function, those coomands can be used and hence increase increase the speed of scan.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

2

Naive solution:

$files = system('find / -printf "%p %b\n"');

Explanation:

  • $files = system(...) Will execute a specified shell command and store the output in the variable $files.
  • find / -printf("%p %b\n") The find command will list all files starting from the filesystem root /. The -printf options tells find to format its output according to the specified format. The format string "%p %b\n" will cause find to output the path of the file (%p), followed by a space, followed by the size of the file in 512 byte blocks (%b), followed by a newline (\n). The -printf options also supports a %k format specifier which will print the size of the file in 1KB blocks.
Thomas Nyman
  • 30,502