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

[eluser]penta997[/eluser]
Hi. I'm trying to upload image file. But when I call an upoading method, the funciton display_errors displaying, that the file wasn't chosen.
contorller:
Code:
function books_validation()
    {


        $this->load->library('form_validation');
        $widok['tytul'] = 'Dodawanie ksiązki do bazy danych';
        $widok['submenu'] = $this->load->view('Admin/submenu_books_view','adasd',true);
           $this->form_validation->set_rules('dzial', 'Dzial', 'trim|required|min_length[4]|max_length[15]');
           $this->form_validation->set_rules('autor', 'Autor', 'trim|required');
           $this->form_validation->set_rules('tytul', 'Tytul', 'trim|required');
           $this->form_validation->set_rules('cena', 'cena', 'trim|required|numeric');
           $this->form_validation->set_rules('wydawnictwo', 'wydawnictwo', 'trim|required');
           $this->form_validation->set_rules('ilosc_str', 'ilosc stron', 'trim|numeric');
           $this->form_validation->set_rules('rok_wydania', 'Regon', 'trim|numeric');
           $this->form_validation->set_rules('opis', 'Opis', 'trim||max_length[3000]');
           $this->form_validation->set_rules('ilosc_szt', 'ilosc sztuk', 'trim|required|numeric');
           $this->form_validation->set_rules('cena_promocyjna', 'cena promocyjna', 'trim|numeric');


           if($this->form_validation->run()==false)
           {
                 //echo
                $widok['main'] = $this->load->view('Admin/add_books_view',array('data'=>$this->Admin_model->get_subcategory()), true);
           }

           else
           {
                $config['upload_path'] = './ksiegarnia/CI/system/application/views/cover/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['is_image'] = 1;
        $config['max_size']    = '300';
        $config['max_width']  = '300';
        $config['max_height']  = '400';

        $this->load->library('upload', $config);
                $query = $this->Admin_model->add_books();
                if($query AND $this->upload->do_upload('okladka'))
                {
                    $info = 'Dodano nowy rekord.';
                    $meta = '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/admin/get_books">';
                    $widok['main'] = $this->load->view('Admin/add_books_info',array('data'=>$info,'meta'=>$meta), true);
                }
                else
                {
                    $errors = $this->upload->display_errors();
                    $data = array('upload_data' => $this->upload->data());
                     $meta = '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/admin/get_books">';
                    $widok['main'] = $this->load->view('Admin/add_books_info',array('data'=>$errors,'meta'=>$meta, 'zaw' => $data), true);

                }
           }

        $this->load->view('Admin/admin_index',$widok);


    }
view:
Code:
<?php echo form_open_multipart('admin/books_validation');?>
<table class="admin_books">
<tr>
    <th colspan="2" style="text-align: center;">Dodaj ksiązke do bazy</th>
</tr>


<tr>
<td>Podkategoria: </td>
<td><select name = "podkategorie">
<option selected>Wybierz podkategorie</option>
&lt;?php foreach ($data->result() as $item)
{
echo '<option>'.$item->SUBC_Name.'</option>';
}
?&gt;
    </select></td>
</tr>
<tr>
<td>Dział: </td><td>&lt;?php echo form_input('dzial');?&gt;</td>
</tr>
<tr>
<td>Autor: </td><td>&lt;?php echo form_input('autor');?&gt;</td>
</tr>
<tr>
<td>Tytuł: </td><td>&lt;?php echo form_input('tytul');?&gt;</td>
</tr>
<tr>
<td>Cena: </td> <td>&lt;?php echo form_input('cena');?&gt;</td>
</tr>
<tr>
<td>Wydawnictwo: </td><td>&lt;?php echo form_input('wydawnictwo');?&gt;</td>
</tr>
<tr>
<tr>
<td>Ilość stron </td><td>&lt;?php echo form_input('ilosc_str');?&gt;</td>
</tr>
<tr>
<tr>
<td>Rok Wydania: </td><td>&lt;?php echo form_input('rok_wydania');?&gt;</td>
</tr>
<tr>
    <td>Opis: </td> <td>&lt;?php
    $data = array(
              'name'        => 'opis',
              'rows'   => '15',
              'cols'        => '24'
            );
    echo form_textarea($data);?&gt;</td>
</tr>
<tr>
<td>Ilość sztuk: </td> <td>&lt;?php echo form_input('ilosc_szt')?&gt;</td>
</tr>
<tr>
<td>Bestseller: </td> <td>&lt;?php echo form_checkbox('bestseller','Bestseller',false)?&gt;</td>
</tr>

<tr>
    <td>Nowość:</td> <td>&lt;?php echo form_checkbox('nowosc','Nowosc',false);?&gt;</td>
</tr>
<tr>
    <td>Promocja</td> <td>&lt;?php echo form_checkbox('promocja','Promocja',false);?&gt;</td>
</tr>
<tr>
<td>Cena promocyjna</td> <td>&lt;?php echo form_input('cena_promocyjna');?&gt;</td>
</tr>
<tr>
    <td>Okładka:</td><td>&lt;input type="file" name="okladka" size="10"/&gt;&lt;/td>
</tr>
<br><br>
<tr>
    <td id="last_td" colspan="2">&lt;?php echo form_submit('dodaj','Dodaj');?&gt;</td>
</tr>

<tr>
    <td id="val_error" colspan="2">&lt;?php echo validation_errors();?&gt;</td>
</tr>

</table>
&lt;?php form_close();?&gt;
#2

[eluser]Jaketoolson[/eluser]
$config['is_image'] isn't something you specify, rather, it's value is returned by the function on success. I'm not sure if removing this would or wouldn't work.

Have you tried breaking this statement into just 1 validation:
Code:
if($query AND $this->upload->do_upload('okladka'))
//to
if($this->upload->do_upload('okladka'))

side note:

your $meta redirects can be rewritten using CI's redirect() function:
Code:
redirect(site_url(), 'refresh');
#3

[eluser]penta997[/eluser]
But I have only one form. How can I send data to validation and separate function upload the same time?
upload function:
Code:
function upload()
    {
        $config['upload_path'] = './ksiegarnia/CI/system/application/views/cover/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['is_image'] = 1;
        $config['max_size']    = '300';
        $config['max_width']  = '300';
        $config['max_height']  = '400';

        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('okladka'))
        {
            $msg = $this->upload->display_errors();
        }
        else
            $msg = 'dodano nowy rekord';
        return $msg;
    }


books_validation function
Code:
else
           {
                $upload = $this->upload();
                $query = $this->Admin_model->add_books();
                if($query)
                {
                    $info = $upload;
                    $meta = '&lt;META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/admin/get_books"&gt;';
                    $widok['main'] = $this->load->view('Admin/add_books_info',array('data'=>$info,'meta'=>$meta), true);
                }
                else
                {
                    $errors = $upload;
                    $data = array('upload_data' => $this->upload->data());
                     $meta = '&lt;META HTTP-EQUIV="Refresh" CONTENT="1; URL='.site_url().'/admin/get_books"&gt;';
                    $widok['main'] = $this->load->view('Admin/add_books_info',array('data'=>$errors,'meta'=>$meta, 'zaw' => $data), true);

                }
           }
#4

[eluser]Jaketoolson[/eluser]
Not quite what I meant. I'm just saying, see whats causing the error. One thought was to remove one of your if statements.
#5

[eluser]penta997[/eluser]
I must check $query variable. When $query return true is mean that data inserted coreclty.
#6

[eluser]Jaketoolson[/eluser]
So whats not working? Your add_books() function, or the upload ($this->upload->do_upload)? Throw your add_books() function out the window until you can get the default upload working! Once that's working then continue....
#7

[eluser]penta997[/eluser]
add_books() is working, I have problem with upload function only.
#8

[eluser]Jaketoolson[/eluser]
did you try removing $config['is_image'] ?
#9

[eluser]penta997[/eluser]
Yes. It's not if condition falt. I've create a new controller and new view:
Code:
&lt;?php echo form_open('test/upload');?&gt;
<tr>
    <td>Okładka:</td><td>&lt;input type="file" name="okladka" size="10"/&gt;&lt;/td>
</tr>
<br><br>
<tr>
    <td id="last_td" colspan="2">&lt;?php echo form_submit('dodaj','Dodaj');?&gt;</td>
</tr>
&lt;?php form_close()?&gt;

contr
Code:
function upload()
   {
              $config['upload_path'] = './ksiegarnia/CI/system/application/views/cover/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';

        $config['max_size']    = '300';
        $config['max_width']  = '300';
        $config['max_height']  = '400';

        $this->load->library('upload', $config);
                if($this->upload->do_upload('okladka'))
                        echo 'wyslano';
                else
                    echo 'nie wyslano';
   }
And it doesnt work too...
#10

[eluser]Jaketoolson[/eluser]
For your test form above, change the form_open to form_open_multipart.

I know you had this originally, but now it wont work regardless...




Theme © iAndrew 2016 - Forum software by © MyBB