Welcome Guest, Not a member yet? Register   Sign In
"You did not select a file to upload " after submitting a form, $_FILES empty, help needed
#1

[eluser]johansonevR[/eluser]
I've tested this code on a separate page
Code:
$config['upload_path'] = './images/uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '10000';
                $this->load->library('upload', $config);
                $this->upload->do_upload('image');
                echo $this->upload->display_errors(); // for debuging
and it works , but when i include it in my index function it does not work why is that ? how to fix it. I have a post variable
Code:
echo $_POST['image']
outputs the value of it. Which tells me it is something inside the function but i don't know what. I've tryed with
Code:
$this->upload->do_upload();
and
Code:
$this->upload->do_upload('image');
But again it doesn't work.
Code:
<?php
class NewAdv extends Controller{
    function NewAdv(){
        parent::Controller();
        $this->load->model('newAdv_model');
        
    }
    function index(){
        $rules=array(
            ...blah blah blah
                );
        $this->form_validation->set_rules($rules);
            if($this->form_validation->run()==FALSE){
                $city['city']=$this->session->userdata('city');
                    $this->load->view('newAdv_view',$city);
                
                
                }else{
                    if($this->input->post('expires')==7){
                        $exp=date('Y-m-d', mktime(0, 0, 0, date("m")  , date("d")+7, date("Y")));
                        }elseif($this->input->post('expires')==14){
                        $exp=date('Y-m-d', mktime(0, 0, 0, date("m")  , date("d")+14, date("Y")));
                        }
                        //This part here does not work
                $config['upload_path'] = './images/uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '10000';
                $this->load->library('upload', $config);
                $this->upload->do_upload('image');
                echo $this->upload->display_errors(); // for debuging
            
                        $today=date("Y-m-d");
                    $data=array(
                    'title'=>$this->input->post('title'),
                    'adv_body'=>$this->input->post('adv_body'),
                    'price'=>$this->input->post('price'),
                    'currency'=>$this->input->post('currency'),
                    'city'=>$this->session->userdata('city'),
                    'category'=>$this->input->post('category'),
                    'created_by'=>$this->session->userdata('username'),
                    'expires'=>$exp,
                    'created'=>$today
                                );
                    $this->newAdv_model->insert($data);
                        sleep(1);
                        redirect('adv/view/'.mysql_insert_id(),'refresh');
                }                    
    }

}
?>
#2

[eluser]xzela[/eluser]
Hi,

You have to use $this->upload->do_upload() in an if statement like this:
Code:
if ($this->upload->do_upload('image') == TRUE) {
   //do more things
}

At least this is how I've been using it. And so far, it seems to be working.
#3

[eluser]johansonevR[/eluser]
ok i've used this code from the guide
Code:
if ( ! $this->upload->do_upload())
                    {
                        $error = array('error' => $this->upload->display_errors());
                        
                        $this->load->view('newAdv_view', $error);
                    }    
                    else
                    {
                        $data = array('upload_data' => $this->upload->data());
                        
                        $this->load->view('adv_view', $data);
                    }
no variable is returned , so the above code doesn't work at all,
could it be because i am using it in a form ?
Code:
if($this->form_validation->run()==FALSE){
                $city['city']=$this->session->userdata('city');
                    $this->load->view('newAdv_view',$city);
                
                
                }else{
                    if($this->input->post('expires')==7){
                        $exp=date('Y-m-d', mktime(0, 0, 0, date("m")  , date("d")+7, date("Y")));
                        }elseif($this->input->post('expires')==14){
                        $exp=date('Y-m-d', mktime(0, 0, 0, date("m")  , date("d")+14, date("Y")));
                        }
                        //This part here does not work
                
                    if ( ! $this->upload->do_upload())
                    {
                        $error = array('error' => $this->upload->display_errors());
                        
                        $this->load->view('newAdv_view', $error);
                    }    
                    else
                    {
                        $picture = array('upload_data' => $this->upload->data());
                        
                        $this->load->view('adv_view', $picture);
                    }
                        
                        $today=date("Y-m-d");
                    $data=array(
                    'title'=>$this->input->post('title'),
                    'adv_body'=>$this->input->post('adv_body'),
                    'price'=>$this->input->post('price'),
                    'currency'=>$this->input->post('currency'),
                    'city'=>$this->session->userdata('city'),
                    'category'=>$this->input->post('category'),
                    'created_by'=>$this->session->userdata('username'),
                    'expires'=>$exp,
                    'created'=>$today
                                );
                    $this->newAdv_model->insert($data);
                        sleep(1);
                        redirect('adv/view/'.mysql_insert_id(),'refresh');
                }
please help i don't want to make another form for picture uploading. I want to use one form for the post data and the picture. I receive data from $_POST['image'] which is the image name respectively.
#4

[eluser]Thorpe Obazee[/eluser]
[quote author="johansonevR" date="1270504543"]
Code:
echo $_POST['image']
outputs the value of it. Which tells me it is something inside the function but i don't know what. I've tryed with
[/quote]

You should have $_FILES['image'] and not $_POST['image'].

Could you show us the form?
#5

[eluser]johansonevR[/eluser]
Code:
print_r($_FILES);
doesn't output anything, so it is empty
here is my form
Code:
<?php
$input=array('title'=>array(
                            'name'=>'title',
                            'type'=>'text',
                            'value'=>set_value('title'),
                            'maxlength'=>'100',
                            'size'=>'100'
                            ),
            'price'=>array(
                            'name'=>'price',
                            'type'=>'text',
                            'value'=>set_value('price'),
                            'maxlength'=>'10',
                            'size'=>'4'
                            ),
            'submit'=>array(
                            'name'=>'submitNewAdv',
                            'type'=>'submit',
                            'value'=>'Publish',
                            ),
            'adv_body'=>array(
                            'name'=>'adv_body',
                            'cols'=>100,
                            'rows'=>15,
                            'value'=>set_value('adv_body')
                            ),
            'pic'        =>array(
                            'name'=>'image',
                            'type'=>'file',
                            )
                        );
?>
<?php if(isset($error)){
    print_r($error);
    }
    ?>
<?php echo form_open_multipart("newAdv",'name=newAdv');?>
<p>Title: &lt;?php echo form_input($input['title']);?&gt; Category: <select name='category'><option value='sell'>Sell</option><option value='buy'>Buy</option></select></p>
&lt;?php echo form_textarea($input['adv_body']);?&gt;
<p>Price: &lt;?php echo form_input($input['price']);?&gt;
<select name='currency' ><option value='LV'>LV</option><option value='EUR'>EUR</option><option value='USD'>USD</option>
</select>Expires in: <select name='expires'><option value='7'>1 week</option><option value='14'>2 week</option></select></p>
Upload a picture: &lt;?php echo form_input($input['pic']);?&gt;
<p>City is automatically set to &lt;?php echo $city;?&gt; &lt;?php echo anchor('','Change City','');?&gt;</p>
&lt;?php echo form_input($input['submit']);?&gt;
&lt;?php echo form_close();?&gt;
&lt;?php echo validation_errors();?&gt;
#6

[eluser]xzela[/eluser]
Although the guide does not explicitly state that the field name is required when using the do_upload() method however, I believe it is in your best interest to specify which form element is doing the uploading. It should be noted that the do_upload() method is looking for a form field named 'userfile'.
From the User Guide:
Quote: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:

try changing this code:
Code:
if ( ! $this->upload->do_upload())
{
    $error = array('error' => $this->upload->display_errors());
    //...

to this:

Code:
if ( ! $this->upload->do_upload('image')) //send the 'image' field to the do_upload method
{
    $error = array('error' => $this->upload->display_errors());
    //...
#7

[eluser]johansonevR[/eluser]
ok even so why my $_FILES array is empty ?
#8

[eluser]adamp1[/eluser]
Can I just check you are using form_open_multipart()? rather than form_open()
#9

[eluser]johansonevR[/eluser]
look at the code i posted above pls.
#10

[eluser]adamp1[/eluser]
Well your code looks fine. The only thing I can think of is maybe somehow the setting file_uploads in php.ini is set to 0. But as you say it does work on another page I don't see how it would be one value for one page and a different value for another.

Edit:
Have you tried sending the postback to a different method other than the index function?




Theme © iAndrew 2016 - Forum software by © MyBB