Welcome Guest, Not a member yet? Register   Sign In
File Uploading
#1

[eluser]Achintha Madushan[/eluser]
when uploading files using codeigniter how I validate the uploading file if exist one?
#2

[eluser]sqwk[/eluser]
You can specify the type of file in "allowed_types": http://ellislab.com/codeigniter/user-gui...ading.html (Scroll down to Preferences)

If you want to make sure that the file input is not empty use form validation:
Code:
$this->form_validation->set_rules('file', 'File', 'required');
More here: http://ellislab.com/codeigniter/user-gui...ation.html
#3

[eluser]Achintha Madushan[/eluser]
No I mean another thing. If my upload folder(destination folder) is "/uploads/". There is allready image in that folder name as image.png. When I upload a file I try to upload file same name (image.png). When do it I want to generate a error you trying to upload file it is all ready saved one in destination path.....thats a my problem.............help me............
#4

[eluser]aquary[/eluser]
CI upload class already rename the file name if the name is already exists, so you don't have to check it, but if you want to:

Code:
if(!file_exists('./uploads/'.$file_name))
   return FALSE;

like that?
#5

[eluser]Achintha Madushan[/eluser]
function add()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'required|callback_name_check');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

if ($this->form_validation->run()==FALSE)
{
$this->load->view('jsValidation');
}else {echo "Success";}


function name_check($str)
{
if ($str == 'Achintha')
{
$this->form_validation->set_message('name_check', 'The %s field is required');
return FALSE;
}
else
{
return TRUE;
}
}
}

According to above controller methode the call back function is not working. I put exit on beggining of call back funtion. but call back funtion not called.......
#6

[eluser]Achintha Madushan[/eluser]
My other question is I have select box in my view. I want get values from database and that values should be set to select box. And also I want to when view is load specify item should be selected......help me..............................
#7

[eluser]sqwk[/eluser]
First things first. You shouldn't echo anything directly in your controllers. While it's not that bad as everybody makes it out to be, your pages will fail miserably as soon as you turn on output compression.

Try this:

Code:
function add() {
    //set validation rules
    $this->form_validation->set_rules(‘name’, ‘Name’, ‘required|callback__name_check’);
    $this->form_validation->set_rules(‘address’, ‘Address’, ‘required’);
    $this->form_validation->set_rules(‘email’, ‘Email’, ‘required’);
    $this->form_validation->set_message('_name_check', 'The %s field cannot be <strong>Achintha</strong>');

    if ($this->form_validation->run() == TRUE) {
        $this->load->view('form_validated);
    } else {
        $this->load->view('normal_view);
    }
}

function _name_check($str) {
    if ($str == 'Achintha')
        return FALSE;
    else
        return TRUE;
}

About getting selected items into a dropdown select…

In your controller
Code:
// Load DB
$this->load->database()

// Get elements from DB -- Normally you would do this in a model
$elements = $this->db->select('id', 'title')->from('table1')-where('do you need a where?')->get()->result();

// Pass to viewfile
$data['elements_for_select'] = $elements;
$this->load->view('normal_view', $data);

In your viewfile: (Somwhere in a form)
Code:
<select name="somename">
    <option value="">Choose one&hellip;</option>
    &lt;?foreach ($elements_for_select as $element): ?&gt;
        <option value="&lt;?=$element->id?&gt;" &lt;?=set_select('somename', $element->id)?&gt;>&lt;?=$element->title?&gt;</option>
    &lt;?endforeach?&gt;
</select>

EDIT: Fixed the _ and the get ;-)
#8

[eluser]Achintha Madushan[/eluser]
select box is ok.... But validation problem call back function is not working. I fill 3 fields and then press submit show success. I add exit; in beginning of _name_check function. But exit segment is not working. it mean call back function not calling. Confusedmirk:
#9

[eluser]Achintha Madushan[/eluser]
$elements = $this->db->select('id', 'title')->from('table1')-where('do you need a where?')->row()->result();

above segment not working
#10

[eluser]sqwk[/eluser]
Sorry, should be ->get()->result() at the end. (get instead of row)

Also, I forgot a _ . Should be:
Code:
$this->form_validation->set_message('_name_check',...

About your exit in the callback function: Nothing returned is read as "everything's fine" or TRUE. Try return FALSE. Also if your three fields are not filled with "Achintha", then it would return TRUE, meaning they validated fine.




Theme © iAndrew 2016 - Forum software by © MyBB