CodeIgniter Forums
Form Generation Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Form Generation Library (/showthread.php?tid=16439)



Form Generation Library - El Forum - 12-16-2009

[eluser]happydude[/eluser]
@hugle

Normally, we set the default upload path for the Form Generator Library in the form.php config folder. In my controller, I want some methodss to upload images to different folder depending on the method.

I want admin/products to upload images to assets/products
I want admin/categories to upload images to assets/categories
I want admin/brands to upload images to assets/brands

What I am looking for is a way to change the file upload path as the case may be in the method.

I don't think FGL allows one to set config on the fly. I then decided to try changing the way the config array is defined, it still didn't work.

Any help will be appreciated. Thanks in advance.


@Mat-Moo

Since are using form generator library to upload, I don't think one has access to file uploading library and cannot really use or chain ->upload(var, label, config)


Form Generation Library - El Forum - 12-16-2009

[eluser]dinhtrung[/eluser]
@happydude: Just upload the file into your /tmp/ folder. Then in the model, you move the file the way you want it. For example:
Code:
/* Controller */
$mymethod = $this->uri->segment(2);
$mymodel = $mymethod.'_m';
$this->form->open()
           ->upload('userfile', 'Please upload something', 'upload_path=/tmp/,allowed_types=png|jpg|gif,max_filesize=4000')
           ->model($mymodel, 'handleupload')
           ->onsuccess('redirect', $this->uri->uri_string());
Then write 3 function handleupload for 3 model file. Or you can add extra parameters after the model() methods like this:
Code:
/* Controller */
    ->model('upload_handle', 'image_upload', array('destination' => '/var/www/devel/assets/images/'))
/* Model : upload_handle.php */
function image_upload(&$form, $data)
{
     $destination_folder = $data['destination'];
     $destination_file = $destination_folder.'/'.$data['upload']['file_name'];
     $source_file = $data['upload']['full_path'];
     rename($source_file, $destination_file);
}
And that's all.


Form Generation Library - El Forum - 12-17-2009

[eluser]Mat-Moo[/eluser]
Why not just set a temp location, and once uploaded move it to the correct location then?


Form Generation Library - El Forum - 12-17-2009

[eluser]macigniter[/eluser]
[quote author="happydude" date="1261042603"]@hugle
Normally, we set the default upload path for the Form Generator Library in the form.php config folder. In my controller, I want some methodss to upload images to different folder depending on the method.

I want admin/products to upload images to assets/products
I want admin/categories to upload images to assets/categories
I want admin/brands to upload images to assets/brands

What I am looking for is a way to change the file upload path as the case may be in the method.[/quote]

This really isn't rocket science ;-) and by the way is clearly documented in the user guide.

I just ran a quick test with the latest pre-release version of the library and have successfully managed to upload two files from two different upload elements within the same form (!) into different folders. All I did was:

Code:
...
->upload('file1', 'Upload File', '', 'upload_path=uploads')
->upload('file2', 'Upload Other File', '', 'upload_path=other_dir')



Form Generation Library - El Forum - 12-17-2009

[eluser]happydude[/eluser]
Funny, I never noticed this previously in the userguide.

Actually, after going through the userguide and mapping stuff into my head, I now rely more on the reference guide which doesn't have config in its syntax.

Thanks for the heads up though.

I've got another problem though. Is there any other way I can access the parameters from the image uploaded asides using the $_FILES array?

This is because spaces are automatically removed from the file uploaded $_FILES['userfile']['name'] still leaves spaces in the name which I'll store in the db and will need to call the stored image later.


Form Generation Library - El Forum - 12-17-2009

[eluser]macigniter[/eluser]
[quote author="happydude" date="1261105568"]
I've got another problem though. Is there any other way I can access the parameters from the image uploaded asides using the $_FILES array?[/quote]

Code:
$this->form->upload('file_upload', 'Upload File');

You can access the data either in the model:

Code:
function do_db_stuff(&$form, $data)
{
   print_r($data['file_upload']);
}

or after $this->form->validate()

Code:
if ($this->form->valid)
{
   $post_data = $this->form->get_post();

   print_r($post_data['file_upload']);
}



Form Generation Library - El Forum - 12-17-2009

[eluser]piyush138[/eluser]
go through this link
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html


Form Generation Library - El Forum - 12-18-2009

[eluser]happydude[/eluser]
[quote author="macigniter" date="1261106016"][quote author="happydude" date="1261105568"]
I've got another problem though. Is there any other way I can access the parameters from the image uploaded asides using the $_FILES array?[/quote]

Code:
$this->form->upload('file_upload', 'Upload File');

You can access the data either in the model:

Code:
function do_db_stuff(&$form, $data)
{
   print_r($data['file_upload']);
}

or after $this->form->validate()

Code:
if ($this->form->valid)
{
   $post_data = $this->form->get_post();

   print_r($post_data['file_upload']);
}
[/quote]

print_r($post_data['file_upload']) is displaying blank.... Nothing whatsoever on my side.

When I did this in my model,
function do_db_stuff(&$form, $data)
{
print_r($data['file_upload']);
}

I got this:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: userfile

Filename: models/product_model.php

Line Number: 12

I'm pretty confused now.

All the while, the images get uploaded, its the parameters that are not returned and I need to store them in the database (They only show up when I use $_FILES but the name is different from that of the file uploaded).


Form Generation Library - El Forum - 12-18-2009

[eluser]macigniter[/eluser]
[quote author="happydude" date="1261168946"]

I'm pretty confused now.

All the while, the images get uploaded, its the parameters that are not returned and I need to store them in the database (They only show up when I use $_FILES but the name is different from that of the file uploaded).[/quote]

Which version of the library are you using?

Please post your controller and model...


Form Generation Library - El Forum - 12-18-2009

[eluser]happydude[/eluser]
I'm using the latest version you provided. 0.2.2 Pre-release

Part of the controller that handles the form and image upload is posted below. Please note that I loaded my model in the constructor like this - $this->load->model('product_model', 'product');

Code:
function add()
    {
        $this->load->library('form');
        
        $this->data['title'] = 'Add Product';
        $this->data['add_type'] = 'Product';
        
        $categories     = $this->admin->prep_for_select('categories');
        $brands            = $this->admin->prep_for_select('brands');
        $sort            = array (1=>1, 2, 3, 4, 5);
        
        $this->form
                 ->open('admin/add')
                ->fieldset()
                ->text('name', 'Product Name', 'trim|required|max_length[255]|xss_clean')
                ->text('reference', 'Product Reference', 'trim|required|numeric|xss_clean')
                ->text('quantity', 'Quantity', 'trim|numeric|xss_clean')
                ->iupload('userfile', 'Product Image')->br()
                ->select('category', $categories, 'Category', '', 'trim|required|xss_clean|callback__check_cat')->nobr()
                ->html('   '.a('admin/addcategory', 'Add a new Category'))->br()
                ->select('brand', $brands, 'Brand', '', 'trim|required|xss_clean|callback__check_brand')->nobr()
                ->html('   '.a('admin/addbrand', 'Add a new Brand'))->br()
                ->textarea('description', 'Description', 'trim|xss_clean')->br()
                ->checkbox('featured', '1','  Feature on Home Page?', FALSE, 'trim|xss_clean')->br()->br()
                ->select('sort', $sort, 'Sort Order', '5', 'trim|xss_clean')
                ->span('e.g. A product with a Sort Order of 3 will be placed higher than one with a sort order of 4 in any listing. It also affects the arrangement of featured products in the sidebar.')->br()->br()
                ->submit('Add Product', 'submit_product')->reset('Clear')
                    ->validate();
                
                    
    
        if ($this->form->valid) {    
            $input = $this->form->get_post();
            
            $added_by = $modified_by = getField('username');
            $date_added = $date_modified = date('Y-m-d H:i:s');
            
            $params = array ( //prep and typecast the data.
                                 'reference_id'     => (int) $input['reference'],
                                 'name'            => (string) $input['name'],
                                 'category_id'    => (int) $input['category'],
                                 'brand_id'        => (int) $input['brand'],
                                 'description'    => (string) $input['description'],
                                 'quantity'        => (int) $input['quantity'],
                                 'featured'        => (int) $input['featured'],
                                 'image'            => (string)$input['userfile']['name'],
                                 'sort'            => (int) $input['sort'],
                                 'added_by'        => $added_by,
                                 'date_added'    => $date_added,
                                 'modified_by'    => $modified_by,
                                 'date_modified'    => $date_modified,                            
                            );
                            
            $result = $this->admin->save_product($params);
            
            if ($result) {
                $this->data['message'] = '<p class="success">Your product has been successfully added. You can add another product below, or '.a('products/'.slug($input['name']).'/'.$result, 'View your recently added product', 'target="_blank"').'.</p>';
            } else {
                $this->data['message'] = '<p class="error">There was a problem adding your product. Please try again later.';
            }                
        }
        
        
        
        $this->data['form'] = $this->form->get();
        $this->data['errors'] = $this->form->errors;        
    }

The model is really not necessary as I use if($this->form->valid) and I do some processing on the data before saving it in the database as you can see in the controller below.

But in order to test the code you provided previously, I commented out the if($this->form->valid) block, changed ->validate(); to model('product_model', 'add') and quickly created the model below. Of course, it gave me the error I posted earlier.

Code:
function add(&$form, $data)
    {
        print_r($data['userfile']);
    }