1

(Well what I gone through a lot of posts here on stackoverflow and other sites. I need a simple task, )

I want to provide my user facility to click on upload file from his account, then select a directory and get the list of all the files names inside that directory.

According to the posts here what I got is I have to pre-define the directory name, which I want to avoid.

Is there a simple way to click a directory and get all the files names in an array in PHP? many thanks in advance!

 $dir = isset($_POST['uploadFile']) ? _SERVER['DOCUMENT_ROOT'].'/'.$_POST['uploadFile'] : null; if ($_POST['uploadFile'] == true) { foreach (glob($dir."/*.mp3") as $filename) { echo $filename; } } 
2
  • 1
    In PHP there is no click event. Also the problem you run into is not clear, you give no reference to the post you try to base your code on nor do you post your code here. That's just not a helpful way to ask a question. Commented Jul 12, 2012 at 17:41
  • also as one of answer says .... if ($handle = opendir(".")) { while (false !== ($file = readdir($handle))) { echo $file."\n"; } closedir($handle); } but it returns the names of the files opened in my netbeans IDE directory, while I want to get the files name from the directory I open by clicking choose file button on the web page ... Commented Jul 12, 2012 at 18:13

5 Answers 5

8

I will go ahead and post a sample of code I am currently using, with a few changes, although I would normally tell you to look it up on google and try it first.

if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { echo $file; } closedir($handle); } 

This will display the entire contents of a directory... including: ".", "..", any sub-directories, and any hidden files. I am sure you can figure out a way to hide those if it is not desirable.

Sign up to request clarification or add additional context in comments.

1 Comment

Can we sort the folders or sub directories first and the files follow?
3
<?php $files=glob("somefolder/*.*"); print_r($files); ?> 

Comments

1

Take a look at the Directory class (here) and readdir()

Comments

1

I'm confused what do you want, all files or only some files? But if you want array of folders and files, do this

$folders = array(); $files = array(); $dir = opendir("path"); for($i=0;false !== ($file = readdir($dir));$i++){ if($file != "." and $file != ".."){ if(is_file($file) $files[] = $file; else $folders[] = $file; } } 

And if only some folders you want, later you can delete them from array

Comments

-1

I always use this amazing code to get file lists:

$THE_PATTERN=$_SERVER["DOCUMENT_ROOT"]."/foldername/*.jpg"; $TheFilesList = @glob($THE_PATTERN); $TheFilesTotal = @count($TheFilesList); $TheFilesTotal = $TheFilesTotal - 1; $TheFileTemp = ""; for ($TheFilex=0; $TheFilex<=$TheFilesTotal; $TheFilex++) { $TheFileTemp = $TheFilesList[$TheFilex]; echo $TheFileTemp . "<br>"; // here you can get full address of files (one by one) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.