Welcome Guest, Not a member yet? Register   Sign In
Multiple file upload library
#21

[eluser]Ghetolobster[/eluser]
Romy,

This saved me a HUGE amount of time thank you. I have however run into a small issue. I want to save the filenames to a database but I cannot seem to figure out how to do so. Could you give me an example of how I would achieve this say with a table such as below


Code:
CREATE TABLE IF NOT EXISTS 'user_images' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'image_name' varchar(100) NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I forgot to mention this is my form

Code:
<?php echo form_open_mutlipart('site/upload');
      echo form_upload('userfile[]','','multiple="multiple"');
      echo form_submit('submit','Upload');
      echo form_close();
?>
#22

[eluser]Romyblack[/eluser]
[quote author="Ghetolobster" date="1339713712"]Romy,

This saved me a HUGE amount of time thank you. I have however run into a small issue. I want to save the filenames to a database but I cannot seem to figure out how to do so. Could you give me an example of how I would achieve this say with a table such as below
[/quote]

Hi Ghetolobster, first at all thanks for taking this library in consideration to your projects.

When you do this proccess.

Code:
$uploaded = $this->upload->up(TRUE);

The $uploaded variable will contain an array with to more arrays, the SUCCESS files and the ERROR. so the SUCCESS contains the files that were uploaded correctly. so the only thing you have to do is to iterate and save it as you desire, for example.

Code:
foreach($uploaded['success'] as $file){
echo $file['file_name'];
}

That's all hope its useful for you. just save that value into your data base Smile
#23

[eluser]Ghetolobster[/eluser]
[quote author="Romyblack" date="1339726997"]

Code:
$uploaded = $this->upload->up(TRUE);

foreach($uploaded['success'] as $file){
echo $file['file_name'];
}
[/quote]

Romy,

Thank you so much for this, I used this code and did the following for entering into the database.

Code:
if (count($uploaded['success']) > 0)
{
    foreach ($uploaded['success'] as $file)
    {
        $data = array (
            'user_id' => $user_id,
            'image_name' => $file['file_name']
        );
  
        $this->db->insert('user_images', $data);
    }
}
#24

[eluser]ogib[/eluser]
Hello everybody,
My file was named MY_Upload, and paste in folder library.
NOT WORKq Message:
Fatal error: Call to undefined method CI_Upload::up() in D:\xampp\htdocs\fpc\application\controllers\Utz_c.php on line 64

Please help
#25

[eluser]Romyblack[/eluser]
[quote author="ogib" date="1344255294"]Hello everybody,
My file was named MY_Upload, and paste in folder library.
NOT WORKq Message:[removed]nullo();
Fatal error: Call to undefined method CI_Upload::up() in D:\xampp\htdocs\fpc\application\controllers\Utz_c.php on line 64

Please help[/quote]

1) Make sure you pasted the file at ./application/libraries/MY_Upload.php
2) Confirm if your code is similar to this one.
Code:
$this->load->library('upload');

$config['upload_path']   = './uploads'; //if the files does not exist it'll be created
$config['allowed_types'] = 'gif|jpg|png|xls|xlsx|php|pdf';
$config['max_size']   = '4000'; //size in kilobytes
$config['encrypt_name']  = TRUE;

$this->upload->initialize($config);

$uploaded = $this->upload->up(TRUE); //Pass true if you want to create the index.php files
3) Verify the config file that this variable has the correct value.
Code:
$config['subclass_prefix'] = 'MY_';
#26

[eluser]ogib[/eluser]
Quote:1) Make sure you pasted the file at ./application/libraries/MY_Upload.php
That was my mistake!
Many thanks!
Works perfectly!
#27

[eluser]Unknown[/eluser]
It is really a special one. I like the way you have presented the information. Interesting post. Thanks for sharing

[url="http://www.wiks.com.au/anemometer"]wind meter[/url]
#28

[eluser]Romyblack[/eluser]
[quote author="Rachel Gawith" date="1344433300"]It is really a special one. I like the way you have presented the information. Interesting post. Thanks for sharing[/quote]

Thank you Rachel, Smile just in case, put romyblack in the search forum field and you'll find some libraries i've build, like the easiest paypal library for CI. Smile
#29

[eluser]Unknown[/eluser]
Hi.. Thank you for making the extended library for uploading files.. I tried to use your extended library, but unfortunately I got an error.. I wonder if you could help me resolve this.. Thanks a bunch..

(This is my error message)
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: -1

Filename: libraries/MY_Upload.php

Line Number: 85

(And this is what I wrote on my controller)
Code:
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = "jpg|png";
$config['max_size'] = '100000';
$config['max_width']  = '6000';
$config['max_height']  = '6000';

$this->load->library('upload', $config);
$uploaded = $this->upload->up(FALSE);
  
foreach($uploaded['success'] as $file){
   echo $file['file_name'];
}


EDIT :
My bad.. The upload path should be like './image/upload' instead of './image/upload/'.. Thx anyway..
#30

[eluser]Pent[/eluser]
Just registered to say this was exactly what I was looking for, thank you very much. I do however run in a minor issue, if there are multiple entries in the "error" array (the files that did not get uploaded) their error messages are concatenated, like this (I'm echoing the file names and the error messages):

(filename1)

The file you are attempting to upload is larger than the permitted size.

(filename2)

The file you are attempting to upload is larger than the permitted size.

The file you are attempting to upload is larger than the permitted size.

So the first error message under filename2 is actually the error message of filename1. I found out this happens because for uploading all the files, one instance of the upload class is used, and the error messages are just added together (because one file can have multiple error messages). It can be fixed by clearing the error array after merging it in the $uploaded_info, like this:
Code:
#Here we do the upload process
   foreach($_FILES as $file => $value){
    if( $value['size'] > 0 ){
     if (!$this->do_upload($file)){
      $uploaded_info['error'][]  =  array_merge($this->data(),
          array('error_msg' => $this->display_errors()));
      $this->error_msg = array();
      
     }
     else{
      $uploaded_info['success'][] =  array_merge($this->data(),
          array('error_msg' => $this->display_errors()));
      $this->error_msg = array();
     }
    }
   }




Theme © iAndrew 2016 - Forum software by © MyBB