Viewed   97 times

Possible Duplicate:
Get the hierarchy of a directory with PHP
Getting the names of all files in a directory with PHP

I have seen functions to list all file in a directory but how can I list all the files in sub-directories too, so it returns an array like?

$files = files("foldername");

So $files is something similar to

array("file.jpg", "blah.word", "name.fileext")

 Answers

5
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename)
{
    // filter out "." and ".."
    if ($filename->isDir()) continue;

    echo "$filenamen";
}


PHP documentation:

  • RecursiveDirectoryIterator
  • RecursiveIteratorIterator
Wednesday, November 16, 2022
5

Because . is the current directory and .. is the parent directory.

They are always exists.

If you need to exclude them - just add

if ($file != '.' && $file != '..')

right before echo

Monday, September 5, 2022
5

Consider the content of your text.txt file

FristLineFirstData  FirstLineSecondData FirstLineThirdData
SecondLineFirstData SecondLineSecondData    SecondLineThirdData

Tab separed.

And the script :

<?php
$file = "text.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("n", $read);//get
$i= 0;//initialize
foreach($lines as $key => $value){
    $cols[$i] = explode("t", $value);
    $i++;
}
echo "<pre>";
print_r($cols); //explore results
echo "</pre>";
?>

will return

Array
(
    [0] => Array
        (
            [0] => FristLineFirstData
            [1] => FirstLineSecondData
            [2] => FirstLineThirdData
        )

    [1] => Array
        (
            [0] => SecondLineFirstData
            [1] => SecondLineSecondData
            [2] => SecondLineThirdData
        )

)
Thursday, November 24, 2022
 
5

Check this out : readdir()

This bit of code should list all entries in a certain directory:

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entryn";
        }
    }

    closedir($handle);
}

Edit: miah's solution is much more elegant than mine, you should use his solution instead.

Monday, September 12, 2022
 
m01
 
m01
1

you can use my updated code and as per my demo it is working perfect for multiple file upload

 <?php
if(isset($_FILES['documents'])){

foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
    $file_name = $key.$_FILES['documents']['name'][$key];
    $file_size =$_FILES['documents']['size'][$key];
    $file_tmp =$_FILES['documents']['tmp_name'][$key];
    $file_type=$_FILES['documents']['type'][$key];  
    move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
 echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";

 echo "</form>";
}
?>
Friday, December 2, 2022
 
lv10
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :