CodeIgniter Forums

Full Version: Trying to put info from files into database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I am trying to scan through a specific folder (which will have images in) and for each one get the information and add them into a database.  These files will already be uploaded onto the server so this is to be able to rebuild the information in the table.

PHP Code:
$this->db->truncate('gallery_images');

 
   $this->load->library('upload');
 
   $files scandir(base_url().'public/images/gallery/');
 
   foreach($files as $file) {
 
       $this->upload->do_upload($file);
 
       $this->session->set_flashdata('success_message'$this->upload->data('file_name'));
 
       $data = array( 
 
           'category' => 'uncategorised',
 
           'image_name' => $this->upload->data('file_name'),
 
           'alt' => $this->upload->data('file_name'),
 
           'full_path' => $this->upload->data('full_path'),
 
           'image_width' => $this->upload->data('image_width'),
 
           'image_height' => $this->upload->data('image_height')
 
       );
 
       $this->add_content($data'gallery_images');

 
   }

 
   redirect('admin/gallery/gallery_images'); 

I have tested it and I know that it's not finding any files but I don't know why.

Thanks,

Doomie22
You cannot use the upload library for files that are already on the server.
You need to use the the directory helper to load the folder's file into an array.
The file helper will give you some the information you want but I don't think you can get the image size, so you would need to use raw PHP or there maybe a library available but that I don't know.
ok atm I am not too bothered about the width and height then.  I have tried the following but I am getting an "Invalid argument supplied for foreach()"


PHP Code:
$this->db->truncate('gallery_images');

 
   $this->load->helper('directory');
 
   $this->load->helper('file');
 
   
    $files 
directory_map(base_url().'public/images/gallery/');
 
   
    foreach
($files as $file) {
 
       
        $get_info 
get_file_info($file);
 
       
        $data 
= array( 
 
           'category' => 'uncategorised',
 
           'image_name' => $get_info['name'],
 
           'alt' => $get_info['name'],
 
           'full_path' => $get_info['server_path'],
 
           'image_width' => '800',
 
           'image_height' => '600'
 
       );
 
       $this->add_content($data'gallery_images');

 
   }
 
   $this->session->set_flashdata('success_message''Gallery Reloaded');
 
   redirect('admin/gallery/gallery_images'); 
You don't need / should not have base_url in the path as this gives you a url (http://domain.ext/yourpath)
You need a physical path.
Per the documentation: Paths are almost always relative to your main index.php file.
So you should call

PHP Code:
$files directory_map('./public/images/gallery/');
// in public is where your index.php file is then 
$files directory_map('./images/gallery/'); 
If you still have an error do a var_dump or print_r of $files to see the content
I am seeing the files in the array but for some reason I am just getting null from this.

PHP Code:
$get_info get_file_info($file); 
Does $file contain the the path or just the file name ?
Get_file_info need the path as well as the file name.
You might be better of using just the file helper.


PHP Code:
$this->load->helper('file');
 
   
    $files 
get_filenames('./images/gallery/',TRUE); //TRUE to include the path with the file name
 
   
    foreach
($files as $file) {
 
       
        $get_info 
get_file_info($file);
 
       
        $data 
= array( 
 
           'category' => 'uncategorised',
 
           'image_name' => $get_info['name'],
 
           'alt' => $get_info['name'],
 
           'full_path' => $get_info['server_path'],
 
           'image_width' => '800',
 
           'image_height' => '600'
 
       );
 
       $this->add_content($data'gallery_images'); 

But I am not sure if this will include sub-folder. you will need to do a var_dump be sure.
Have a look at the helpers documentation to see what is available.
Maybe this will help, not CI but works.

Read: 3. Recursive Directory Listing

The Art of Web