Welcome Guest, Not a member yet? Register   Sign In
image upload
#1

[eluser]praveenarya[/eluser]
hi all,
i have to upload image from page which looks like this
Code:
table width="90%" border="0" align="center" cellpadding="4" cellspacing="1" class="sborder">
            <form id="news_mana" name="news_mana" method="post" action="">
              <tr>
                <td>&nbsp;</td>
                <td align="center">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td width="200" align="right">News Title<span class="red"> * </span></td>
                <td align="center"><strong>:</strong></td>
                <td>&lt;input name="news_head" type="text" class="Inputform" id="news_head" size="70" /&gt;&lt;/td>
              </tr>
              <tr>
                <td align="right" nowrap>Image</td>
                <td align="center"><strong>:</strong></td>
                <td>
                    &lt;input name="img" type="file" class="Inputform" id="img" size="35" value=""/&gt;
                    &lt;? if(!empty($_GET['nid'])){?&gt;
                    &lt;input type="hidden" name="oldimg" id="oldimg" value="&lt;?=$row_news['n_image']?&gt;" &gt;
                  <strong><a  class="insub">View</a></strong>
                  &lt;? } ?&gt;<br>                </td>
              </tr>
              <tr>
                <td align="right" nowrap>Keywords</td>
                <td align="center"><strong>:</strong></td>
                <td>&lt;textarea name="n_key" cols="40" rows="3" id="n_key"&gt;&lt;/textarea></td>
              </tr>
              <tr>
                <td align="right" nowrap>Description</td>
                <td align="left" nowrap><strong>:</strong></td>
                <td align="left" nowrap>&nbsp;</td>
              </tr>
              <tr>
                <td colspan="3" align="right" nowrap>&lt;textarea name="n_content" cols="80" rows="15" id="n_content&gt;&lt;/textarea></td>
                </tr>
              <tr>
                <td colspan="3" align="center">&lt;input name="sub" type="submit" class="Buttons "  value="Submit"/&gt;&lt;/td>
              </tr>
            &lt;/form&gt;
          </table>
and in my controller
my code looks like this
Code:
&lt;?php

class Add_news extends Controller {

function index()
    {
    $this->load->view('admin/add_news');
    if($_SERVER['REQUEST_METHOD']=='POST'){

        $upload = array
        (
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png'
        );

        $this->load->library('upload', $upload);

             $news_manage="insert into ab_news(n_head, n_author, n_image, n_keywords, n_desc, n_status, n_dateadded) VALUES('".$_POST['news_head']."','".$_POST['news_author']."','".$pro_imgpath."', '".$_POST['n_key']."','".$_POST['n_content']."','1',now())";
mysql_query($news_manage);
        redirect('apna_bus_cp/manage_bus','location');
            
}
    }

    
    
function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            $this->load->view('upload_form', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            $this->load->view('upload_success', $data);
        }
    }    
    }
i know iam doing it wrong but cant able to find it where please help me to fix this
#2

[eluser]BrianDHall[/eluser]
Here you go, from the manual on file uploading:
Quote:$this->upload->do_upload()

Performs the upload based on the preferences you've set. Note: By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type:

&lt;form method="post" action="some_action" enctype="multipart/form-data" /&gt;
If you would like to set your own field name simply pass its value to the do_upload function:

$field_name = "some_field_name";
$this->upload->do_upload($field_name)

In your do_upload call you aren't passing a field name, but your file upload field is not called 'userfile' - so CI is looking for a posted file field that just doesn't exist.

I think thats the only problem, I can't see anything else wrong.
#3

[eluser]praveenarya[/eluser]
thanks for the reply it worked but i have a doubt what if i have two image fields one for original image size and one field for thumbnail image size do i need to write like this
Code:
$upload = array
        (
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png'
        );

        $this->load->library('upload', $upload);
        $this->upload->do_upload('img');

$upload2=array
              (
                 'upload_path'   => './uploads/thumbs',
            'allowed_types' => 'gif|jpg|png',
                     'max_width'=>'125',
                    'max_height'=>'90'
                   );
$this->load->library('upload', $upload2);
        $this->upload->do_upload('thumbs');
correct me if iam wrong
thanks
#4

[eluser]BrianDHall[/eluser]
I'm afraid I'm personally not sure about doing multiple file uploads, as I use an unlimited file upload approach where I have Flash/javascript upload files one at a time to a script.

However, you might want to make things more magical by using the image manipulation class: http://ellislab.com/codeigniter/user-gui...e_lib.html

This way you can upload the big image and create the thumbnail automagically.

However you might still want multiple files to be uploaded in one form - I just don't know that answer Smile




Theme © iAndrew 2016 - Forum software by © MyBB