简体   繁体   中英

How to save url images in database from assets in codigniter?

I saved the images in ../assets/images/ and I want to save links for each image in the database that can display them in view with foreach. How do I save all the images in the database so for each to have an id?

Assuming the problem is that you don't know from which URL / dir-level the images will be called, use absolute path in your views and it won't matter:

<img src='/assets/images/123.jpg' />

Edit. I guess my assumption was wrong. You don't need a DB to just display them. If you do, amend the code below to insert them into the DB where it echos the image tag instead.

The followiung will loop over your directory, grab jpg, png and gif files (as long as they have lowercase extension) and creates appropriate HTML image tags.

        $path = 'assets/images';

        $img = array();
        if ($handle = opendir('.'.$path)) {
            while (false !== ($entry = readdir($handle))) {
                $n++;
                if  (       (substr($entry,-4)=='.jpg') || (substr($entry,0,4)!='.gif') || (substr($entry,0,4)!='.png') ){
                    $img[$n]=$entry;    
                }
            }
            closedir($handle);
        }

        foreach($img as $v){
            echo "<img src='$path$v' />";
        }

Generally, try to describe your problems better and show some work you have already done before asking a question here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM