Welcome Guest, Not a member yet? Register   Sign In
how to read files from the directory without get_filesnames()
#1

[eluser]linderox[/eluser]
I want to make a cool my own photogallery
I want to upload to directory some image files and read directory with files to the database.
But I have 2 problems:
1) get_filenames function reads files from the subdirectory folder (e.g. thumbs) and I get more than I want to get lines to my db.
2) I don't know how to add character $img_suffix to the place before type of the file.
I want to read SomeImgFile.jpg and than automaticaly add to the database place of the thumbfile SomeImageFile_cr.jpg

here is my code with my ideas.Can you help me to solve this problems or give me your ideas how to update my database with list of files.

Code:
function FillUpDatabaseWithPhoto($directory)
    {

        $pathtothumb = '/sm/';
        $imgsuffix = '_cr';
        
        $this->load->helper('file');
        $this->load->database('photostock');        
        $filesarray=get_filenames("public/images/$directory",FALSE);
        echo $filesarray." <br>\n" ;
        foreach ($filesarray as $imgfile) {
            $array = array('originalpath' => "$directory/$imgfile", 'thumbpath' => "$directory.$pathtothumb.$imgfile.$imgsuffix", 'gid' => "$directory");
            $this->db->set($array);
            $this->db->insert('photostock');
            /*$this->gallery_model->additem($data); */
            echo "$imgfile \n <br>";
            }
        
        
    
    }

here what i should get in my database. I have 12 files and thumb directory inside with 12 thumb files
Code:
050sq.jpg      050sq_cr.jpg
but I get in my database such thing. I get 24 rows with stuff like this:
Code:
050sq.jpg      050sq.jpg_cr        // added "_cr" to the end, but I need "_cr.jpg"
050sq_cr.jpg      050sq_cr.jpg_cr // excess line
#2

[eluser]TheFuzzy0ne[/eluser]
Why you need to use the suffix? Would putting the files in a directory named "cr" not suffice?
#3

[eluser]linderox[/eluser]
i have to put files to a separate dir and I have to rename it by adding suffix
#4

[eluser]TheFuzzy0ne[/eluser]
Here's how I would do it, although I can think of at least 10 more ways of doing it.

Code:
&lt;?php

function append_suffix($filename, $suffix="")
{
    $filename = explode('.', $filename);
    $filename[count($filename)-2] = $filename[count($filename)-2] . $suffix;
    return implode('.', $filename);
}

$suffix = '_cr';
$filename = "somefile.jpg";
echo append_suffix($filename, $suffix); # echoes somefile_cr.jpg




Theme © iAndrew 2016 - Forum software by © MyBB