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

Hi,

Finally I have uploaded files on amazon s3, but problem is my files are in folder home and glod() shows path like

/home/ankur123.zip

 

So problem is it has created folder home on s3 which I dont want.

how can I print only name of file ?

Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.