image upload - El Forum - 09-16-2009
[eluser]praveenarya[/eluser]
hi all,
i have to upload image from page which looks like thisCode: 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> </td>
<td align="center"> </td>
<td> </td>
</tr>
<tr>
<td width="200" align="right">News Title<span class="red"> * </span></td>
<td align="center"><strong>:</strong></td>
<td><input name="news_head" type="text" class="Inputform" id="news_head" size="70" /></td>
</tr>
<tr>
<td align="right" nowrap>Image</td>
<td align="center"><strong>:</strong></td>
<td>
<input name="img" type="file" class="Inputform" id="img" size="35" value=""/>
<? if(!empty($_GET['nid'])){?>
<input type="hidden" name="oldimg" id="oldimg" value="<?=$row_news['n_image']?>" >
<strong><a class="insub">View</a></strong>
<? } ?><br> </td>
</tr>
<tr>
<td align="right" nowrap>Keywords</td>
<td align="center"><strong>:</strong></td>
<td><textarea name="n_key" cols="40" rows="3" id="n_key"></textarea></td>
</tr>
<tr>
<td align="right" nowrap>Description</td>
<td align="left" nowrap><strong>:</strong></td>
<td align="left" nowrap> </td>
</tr>
<tr>
<td colspan="3" align="right" nowrap><textarea name="n_content" cols="80" rows="15" id="n_content></textarea></td>
</tr>
<tr>
<td colspan="3" align="center"><input name="sub" type="submit" class="Buttons " value="Submit"/></td>
</tr>
</form>
</table>
and in my controller
my code looks like thisCode: <?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
image upload - El Forum - 09-16-2009
[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:
<form method="post" action="some_action" enctype="multipart/form-data" />
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.
image upload - El Forum - 09-16-2009
[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
image upload - El Forum - 09-17-2009
[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-guide/libraries/image_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
|