Welcome Guest, Not a member yet? Register   Sign In
!$this->upload->do_upload() - first time around
#1

[eluser]nevsie[/eluser]
Straight out of CI guide:
Code:
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);
}

Now when i place this in a controller and then load the sample view provided - enter the URL into the browser - i am immediately told that no file has been uploaded... correct, as i have not even submitted the form yet!
Surely this should check for a POST before setting the $this->upload->display_errors() to say no file has been uploaded - at least this would be logical in relation to how standard form validation works???

Or am i missing the obvious in that no form would typically only be a file upload and therefore some other validation and checks would be done???
But in this case what would you suggest -
check for a POST myself - what is the most sensible CI way of doing this?
#2

[eluser]flaky[/eluser]
you've probably put the code in the constructor or index, remove it from there and do a check if a post has been made
#3

[eluser]nevsie[/eluser]
sorry i might be misunderstanding you here???
but the sample code from the above is not in the constructor or index function, but the function specifically related to the view that holds the right form field. So when the class/function with the above code in it is first called it obviously puts a fail into $error, and then error gets loaded into the view and then displays.

My point was that shouldn't $this->upload->do_upload() act in the same way as any other form validation?
or is it expecting me to check for the post first? If it is expecting me to check for the post first then what would be the most sensible way?

Thanks for trying to help, N
#4

[eluser]flaky[/eluser]
put the code here, so I can help you

based on what you wrote
Quote:Now when i place this in a controller and then load the sample view provided - enter the URL into the browser - i am immediately told that no file has been uploaded… correct, as i have not even submitted the form yet!
I come to the conclusion that this code is inside the contructor or index, like this
Code:
//it is in the construct
    public function __construct(){
        //it is here
        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);
        }
    }

    //or it is in the index
    public function index(){
        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);
        }
    }

cheers
#5

[eluser]adamp1[/eluser]
Well it does check for if there are files uploaded. Below is the first few lines of $this->upload->do_upload() method.
Code:
// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]))
{
    $this->set_error('upload_no_file_selected');
    return FALSE;
}

@flaky: He's already said its not in either of those and anyway that shouldn't matter. The check still gets run for if the $_FILES variable contains file details.
#6

[eluser]nevsie[/eluser]
Hi All and thanks again. for helping...
Flaky - the code i am using is literally the demo code straight out of the userguide for both the controller and view files - this is why i have not repeated it here.

Adamp1 - This is kind of my point! The first thing it does is check the file field for a file and then automatically set an error message. Should this not check for an overall POST before checking the file field? If there has been no POST, then there will be no file, hence no need to set this first error message.

Anyhow, i have setup my own POST check now outside of the do_upload to get the desired effect - unfortunately i am using a hidden form field, then checking to see if that was posted - i know this is messy compared to checking the POST direct, etc, but i could not set up a reliable way that would deal with Empty POST's on submit, after errors, and if the user returns to this page to modify the fields they have already set...

Also Adamp1 - you avatar is scarily jogging my mind to think IE!!!




Theme © iAndrew 2016 - Forum software by © MyBB