Welcome Guest, Not a member yet? Register   Sign In
Pass file name, size, date and path from controller to view with file helper and get_file_info?
#1

[eluser]mci[/eluser]
Hi

I'm new to CI and I have the following code in my controller to list the files in my view.

Code:
$data['files'] = directory_map('./user_data/');
$this->load->view('files', $data);

Now I need size, date and path of every file.
I think the easiest way would be to use File Helper an get_file_info.
But I can't figure out how I should extend my code to pass size, date and path to my view.

Any help would be appreciated.
#2

[eluser]sooner[/eluser]
if the directory_map returns all the files...then you can loop through the files. ie foreach($data['files'] as $file) then inside the loop you can use the php functions to get information for size,date and path...see php function list to see which one you need...then you can save all those info in separate array index for eg : $data['date']['somerandomvalue']...likewise for size $data['size']['somerandomvalue']....in the view you can access the date by looping through $date......here 'somerandomvalue' means u need to allocate a unique value so that when you are looping ($data['files'])through the code..it get saved in different array index rather than over writing it.
#3

[eluser]mci[/eluser]
Now my code look's like this, but I think that your answer sounds better than my solution.
But I don't understand exactly how the code should look like with your suggestion.

Code:
$counter = 0;
        
        $map = directory_map('./user_data/');
        
        foreach($map as $file) {
            
            $files[$counter] = get_file_info('./user_data/'.$file);
            $counter = $counter + 1;
        }
        
        //print_r($files);
        
        $data['files'] = $files;
        
        $this->load->view('files', $data);
#4

[eluser]sooner[/eluser]
Code:
$counter = 0;
        
        $map = directory_map('./user_data/');
        
        foreach($map as $file) {
            
            $files[$counter] = get_file_info('./user_data/'.$file);
             $filesize[$counter] = filesize($file);
            $counter = $counter + 1;
        }
        
        //print_r($files);
        
        $data['files'] = $files;
        $data['filesize'] = $filesize;
        $this->load->view('files', $data);

in the view you can access by $files,$filesize...is that what you are asking?
#5

[eluser]mci[/eluser]
Ok, thank you. I'm asking myself if this is the best/correct solution or would you do it in a different way, maybe becaus of performance, better coding or whatever.?




Theme © iAndrew 2016 - Forum software by © MyBB