dsa45132 2018-09-16 14:51
浏览 134

Codeigniter图像上传到服务器文件夹

I am new to image uploading and would like to request for help about uploading file image into server folder . I am using ajax to send the image to my controller . This is what i received when i var_dump it in the controller. After calling $_FILES['croppedImage'] The result i get is

array(5) { ["name"]=> string(4) "blob" ["type"]=> string(10) "image/jpeg" 
["tmp_name"]=> string(14) "/tmp/phpqtmMQu" ["error"]=> int(0) ["size"]=> 
int(10686) }

This is my controller function that i use to upload my image into the server folder.

  public function upload_base_64(){

    $MY_UPLOAD_DIR = "alert_images/";

    $imagefilename = basename( $_FILES['croppedImage']['name']);

    $fulluploadpath =base_url().'/uploads/'.$MY_UPLOAD_DIR. $imagefilename;
    $image_name = strip_tags($_FILES['croppedImage']['name']);
    $image_size = getimagesize($_FILES['croppedImage']['tmp_name']);

    if($image_size == FALSE) {
        echo 'The image was not uploaded';
    } 
    else if(move_uploaded_file($_FILES['croppedImage']['tmp_name'], $fulluploadpath )) {
        var_dump('it works');exit;

    } else {
        echo "Sorry, there was a problem uploading your file.";
    }
}

It went to the else condition . I saw a few tutorial having this code but i do not know how to use it and why does it only require name?Doesn't it need the file?

    public function uploadfeaturedfile()
   {
    $dir_name                   = 'alert_images/';
    $config['upload_path']      = './uploads/'.$dir_name;
    $config['allowed_types']    = 'gif|jpg|png|jpeg';
    $config['max_size']         = '5120';
    $config['min_width']        = '640';
    $config['min_height']       = '640';

    $this->load->library('dbcupload', $config);
    $this->dbcupload->display_errors('', '');

    if($this->dbcupload->do_upload('photoimg'))
    {
        $data = $this->dbcupload->data();
        $this->load->helper('date');

        $format = 'DATE_RFC822';
        $time = time();
        create_rectangle_thumb('./uploads/'.$dir_name.$data['file_name'],'./uploads/thumbs/');

        $media['media_name']        = $data['file_name'];
        $media['media_url']         = base_url().'uploads/'.$dir_name.$data['file_name'];
        $media['create_time']       = standard_date($format, $time);
        $media['status']            = 1;

        $status['error']    = 0;
        $status['name'] = $data['file_name'];
    }else{
        $errors = $this->dbcupload->display_errors();
        $errors = str_replace('<p>','',$errors);
        $errors = str_replace('</p>','',$errors);
        $status = array('error'=>$errors,'name'=>'');
    }
    echo json_encode($status);
    die;
}  

All help are really appreciated!!! Please and thanks in advance.

  • 写回答

1条回答 默认 最新

  • dounei9043 2018-09-17 06:03
    关注

    This is pretty much verbatim from the docs:

        $config['upload_path'] = './uploads/alert_images/'; // folders have to exist
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('blob')) {
            show_error($this->upload->display_errors());
        } else {
            print_r($this->upload->data());
        }
    

    https://www.codeigniter.com/userguide3/libraries/file_uploading.html

    I suspect your code wasn't working because you need a proper (relative) file path (you can't use a url); more on that here: https://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

    评论

报告相同问题?