“did not select a file to upload” when uploading u

2019-02-22 03:52发布

问题:

im trying to upload an image but it always give me "You did not select a file to upload."

My controller

function add()
{

        $thedate=date('Y/n/j h:i:s');
        $replace = array(":"," ","/");
        $newname=str_ireplace($replace, "-", $thedate);

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['file_name']=$newname;
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);
        //$this->upload->initialize($config);
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','title','trim|required');
        $this->form_validation->set_rules('description','Description','trim|required');
        $image1=$this->input->post('image');


     if ($this->form_validation->run()==FALSE){

            $this->addview();   

            return false;

        }


      if (!$this->upload->do_upload($image1)) {

        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_error', $error);


         }

       else {
        $mage=$this->upload->do_upload($image1);

            $data =array(
            'title'=>$this->input->post('title'),
            'descrip'=>$this->input->post('description'),

            'image' => $mage['file_name']

    );  


            $this->load->model('member_functions');

            $q=$this->member_functions->insert($data);
    }}

all the file requirements and the file permissions are set but i still get the that eror. can someone please tell me what am i doing wrong

回答1:

Parameter to $this->upload->do_upload() function should be the name of the form field. (If you call it without parameters userfile would be used). It seems in your case it should be 'image'. Instead of:

$image1=$this->input->post('image');

it should be:

$image1='image';


回答2:

  • View page

form tag should contain enctype="multipart/form-data".
ie, <form action='' method=''enctype="multipart/form-data> <input type='file' name='field_name'/>

  • controller

and in controller upload code should be $this->upload->do_upload("field_name")
and jsut check whether file reaches in server side by making just print like

print_r($_FILES);

if you get null array then make sure that client side code is correct.



回答3:

I totally agree with the advice about making sure the name of the file element in the form is either userfile or, if different, is passed to the do_upload method. However, for future reference it's also worth noting this line:

$image1=$this->input->post('image');

comes before

if (!$this->upload->do_upload($image1)) {

I found the post method of the input class does not return anything until after the do_upload method is called. Also, you've already called it in the if statement so you don't need to call it again in the else clause. After calling do_upload you now have access to form elements with $this->input->post and information about the uploaded file with $this->upload->data()



回答4:

make sure you use

form_open_multipart()

if you are using the form helper.



回答5:

This is all too familiar a problem.

I'm going to add my answer as a fuller explanation to another answer I found here. The code I am submitting is not different code, but rather adds some lines and specifics not mentioned in his answer.

Even though it looks like his answer is technically correct, and perhaps the answers above his are also correct in their own way, they didn't work for me. I had to study this problem to find out what is really going on, and I learned enough about the process to understand how to write my own solution.

First of all, refer to Nana Partykar's comment, "In your controller, i'm not able to see any is_uploaded_file() function ?" That comment tells us that people are misunderstanding the two files which have similar names, but are different. I know, because for a while I thought they must be referring to the same file, the controller file (named "Uploader.php"). I can see almost all of these questions refer to the same "How to upload multiple files using Ajax" tutorial somewhere out there, my own version, included. The code we are all using is exactly the same.

But, the controller file is "Uploader.php". Where you see $this->upload->do_upload() or $this->upload->do_upload('userfile') or even $this->upload->do_upload('files'), this is referring to a system/library module file named "Upload.php". Notice that before you call the do_upload() function, you have to invoke this line: $this->load->library('upload', $config);

Sachin Marwha gave us a for loop that loops through the $_FILES['userfile'] array. Let's say you upload three pictures. Each $_FILES['userfile'] element is itself made up of 5 "properties": name, type, tmp_name, error, size. You can see these $_FILE properties on PHP.

You only want to pass one file at a time to do_upload(). You don't want to pass all three (or even 20) files at a time to do_upload. That means you have to break down the $_FILES['userfile'] array into individual files before you call do_upload(). To do this, I make a $_FILES['f'] element of the $_FILES array. I figured this out by setting breakpoints in the system/library/Upload.php file, in the do_upload($file = 'userfile') function, to see where I was getting the infamous “did not select a file to upload” that everybody (including myself) keeps complaining about. That function, you will find, uses the original $_FILES array your form sends to your controller. But it really only uses the name of the input type=file from your form. If you don't tell it the name of the form input, it will default to $_FILES['userfile']. As it turns out, that was my biggest problem, because if I used the name of my input field, well, that field passes an array or a collection of files, not just a single file. So I had to make a special $_FILES['f] element, and ONLY pass $_FILES['f'].

Here's the way I am doing it, and believe me, I tried all of the versions on this page and others, not just one StackOverflow, but other tutorials, as well:

    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i < $cpt; $i++)
    {
        unset($config);
        $config = array();
        $config['upload_path']   = $path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = FALSE;
        $config['file_name'] = $_FILES['userfile']['name'][$i];

        // Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop.
        $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
        $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
        $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
        $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
        $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];

        $this->load->library('upload', $config);
        $this->upload->initialize($config);            
        if (! $this->upload->do_upload('f'))
        {
            $data['errors'] = $this->upload->display_errors();
        }
        else
        {
            $data['errors'] = "SUCCESS";
        }

        unset($config);
        $config = array();
        $config['image_library']    = 'gd2';
        $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;
        $config['thumb_marker']     = '.thumb';
        $config['width']            = 100;
        $config['height']           = 100;

        $this->load->library('image_lib', $config);
        $this->image_lib->clear();
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $types = array('.jpg');
    }        

Where it unsets the $config array within the for i loop, and then remakes the $config array, that's the part that makes the thumbnail images for each picture file.

The Complete Controller Upload Function:

    public function upload_asset_photo()
    {
        $data = array();
        $dateArray = explode("/",$this->input->post('date'));
        $date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day
        $cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys.
        $padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003"
        $path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path
        if (!is_dir($path)) {
            mkdir($path,0755,true); //makes the ile path, if it doesn't exist
        }

        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i < $cpt; $i++)
        {
            unset($config);
            $config = array();
            $config['upload_path']   = $path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = FALSE;
            $config['file_name'] = $_FILES['userfile']['name'][$i];

            $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
            $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
            $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
            $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
            $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];

            $this->load->library('upload', $config);
            $this->upload->initialize($config);            
            if (! $this->upload->do_upload('f'))
            {
                $data['errors'] = $this->upload->display_errors();
            }
            else
            {
                $data['errors'] = "SUCCESS";
            }

            unset($config);
            $config = array();
            $config['image_library']    = 'gd2';
            $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
            $config['create_thumb']     = TRUE;
            $config['maintain_ratio']   = TRUE;
            $config['thumb_marker']     = '.thumb';
            $config['width']            = 100;
            $config['height']           = 100;

            $this->load->library('image_lib', $config);
            $this->image_lib->clear();
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $types = array('.jpg');
        }        

        header('Content-Type: application/json');
        echo json_encode($data);
    }