Domanda

I changed and now it does not show nothing. The directories exist on the folder.

i am giving you an example. If i echo $dir i get fullurlpath/root_folder/username

the whole path and directories exist so the $dir is ok, something else must happend.

class Front_Page extends CI_Model{

function front_display(){

    $this->db->select('id, username');
    $query = $this->db->get('models')->result();

    $display_models = '<ul class="display-models">';

    foreach ($query as $row){


        $username= $row->username;

        $dir = base_url('/img/'.$username.'');
        echo $dir;
        if(is_dir($dir)){
            if($dh = opendir($dir)){
                while(($file = readdir($dh)) !== FALSE){            

                    $display_models .= '<li>';
                    $display_models .= '<a href="profilepage/'.$row->id.'">';
                    $display_models .= '<img src="'.$dir.'/'.$file.'">';
                    $display_models .= '</a>';
                    $display_models .= '</li>';

                }

                closedir($dh);
            }
        }
    }

        $display_models .= '</ul>';

        return $display_models;
}

}

È stato utile?

Soluzione

The error you are getting is sayings the path is not found, it has to exists first. The second problem is that you are using the code the wrong way even when the path is fixed.

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $display_models .= '<li>';
    $display_models .= '<a href="profilepage/'.$row->id.'">';
    $display_models .= '<img src="'. $dir.'/'. $file.'">';
    $display_models .= '</a>';
    $display_models .= '</li>';
        }
        closedir($dh);
    }
}

You will want something similar to the above, that is how you use that function.

Remember that the path needs to be fixed first otherwise this will not loop through anything because the folder was not found and it will not find any files to loop through.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top