Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Doing simple ftp viewer in php:

i've got a function that returns arrays with file_name.

I do (in the end of my function) print_r($files); and this is out put

Array ( [0] => task_manager.zip [1] => 1086237 [2] => zip ) Array ( [0] => asd.zip [1] => 1086237 [2] => zip [3] => fonts/some_file.zip [4] => 959224 [5] => zip )

As you can see i get arrays, and this is perfect but then i want to get file_name, file_size & file_extension and I get fail:

p.s - ListFiles - is my function name, ('fonts/') - folder where i find files

foreach (ListFiles('fonts/') as $key=>$file){
    echo 'file_name = '.$file."<br/>";
}

and I get:

file_name = task_manager.zip
file_name = 1086237
file_name = zip
file_name = fonts/some_file.zip
file_name = 959224
file_name = zip

which actually is wrong, because i want do it in one line, something like this:

WHAT I WANT (NEAR THE PERFECT WAY) TO DO SOMETHING LIKE THIS:

in php:

foreach (ListFiles('fonts/') as $key=>$file){
    echo 'file_name = '.$file[0].'file_size = '.$file[1].'file_ext = '.$file[2].'<br/>';
}

in html:

file_name = task_manager.zip, file_size = 1086237, file_ext = zip
file_name = some_file.zip, file_size = 959224, file_ext = zip

UPDATE: MY ListFiles function

function ListFiles($dir) {
    if($dh = opendir($dir)) {

                //This data I get from form
        $allowedExts = array();
        if(isset($_POST['exts'])){
            for ( $i=0; $i < count($_POST['exts']); $i++ ){
                array_push($allowedExts, $_POST['exts'][$i]);
            }
        } // end of this data

        $files = Array();
        $inner_files = Array();
        while($file = readdir($dh)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($dir . "/" . $file)) {
                    $inner_files = ListFiles($dir . "" . $file);
                    if(is_array($inner_files))
                        $files = array_merge($files, $inner_files); 
                } else {
                    if(in_array(end(explode('.', $file)), $allowedExts)){
                        $filesize = filesize('./'.$dir.$file);
                        $userfile_extn = substr($file, strrpos($file, '.')+1);
                        array_push($files, $dir . "/" . $file, $filesize, $userfile_extn);

                    }                
                }
            }
        }

        closedir($dh);
        return $files;
    }
}
share|improve this question
 
How about showing the source code of your ListFiles function? –  DCoder Jul 28 '12 at 16:14
 
We need your ListFiles() function because there is a better way to setup your array for what you're trying to do. –  David Jul 28 '12 at 16:16
 
added, sorry me for this.. –  Johhny Galovecki Jul 28 '12 at 16:22
1  
Rewrite the array_push($files, $dir . "/" . $file, $filesize, $userfile_extn); line as $files []= array($dir . "/" . $file, $filesize, $userfile_extn);. –  DCoder Jul 28 '12 at 16:53
 
man, you are awesome:) I've made a stupid mistake and you foound it! NICE!!!! –  Johhny Galovecki Jul 28 '12 at 16:57
add comment

1 Answer

Simplest way:

$files = ListFiles('fonts/');
for ($i = 0; $i < count($files); $i += 3){
    echo 'file_name = '.$files[$i].'file_size = '.$files[$i+1].'file_ext = '.$files[$i+2].'<br/>';
}

Better way:

http://pastie.org/4567820

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.