Jump to content

How to store file names derived from glob() in variable array


ankur0101

Recommended Posts

Hi,

I want to store files to variable array using glob()

 

like

$files[0] = xyz.txt

$files[1] = pqr.txt

.

.

.

$files[n] = nfile.txt

 

I know how to list files from directory using glob()

 

<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>


 

How can I do that ?

 

Link to comment
Share on other sites

No,

What I mean s if there are 3 files, then PHP will first count number of files. (May be using for loop or whatever)

Then at every count it will do $count++ so as to know how many files exist i.e. number of files

 

Then

$files[0] = xyz.txt

$files[1] = pqr.txt

.

.

.

$files[n] = nfile.txt

Link to comment
Share on other sites

No,

What I mean s if there are 3 files, then PHP will first count number of files. (May be using for loop or whatever)

Then at every count it will do $count++ so as to know how many files exist i.e. number of files

 

Then

$files[0] = xyz.txt

$files[1] = pqr.txt

.

.

.

$files[n] = nfile.txt

 

You shouldn't say "No" without testing. Zane's code does exactly what your example shows us you want.

Link to comment
Share on other sites

Hi,

It worked.

here is my code.

<?php
$files_in_directory = "home/";
$files = array();

foreach (glob($files_in_directory."*.zip") as $filename) {
    $files[] = $filename;
}
$counter = count($files);
echo "<p>$counter</p>";
print_r($files);

for($i=0; $i<$counter; $i++)
{
    echo "<p>$files[$i]</p>";
    }

?>

 

Can I use $files[$i] to upload file using FOR LOOP ?

 

 

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.