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

问题描述 投票:1回答:1

我是图像上传的新手,想请求有关将文件图像上传到服务器文件夹的帮助。我正在使用ajax将图像发送到我的控制器。这是我在控制器中var_dump时收到的内容。在调用$ _FILES ['croppedImage']之后我得到的结果是

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

这是我用来将我的图像上传到服务器文件夹的控制器功能。

  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.";
    }
}

它进入了其他条件。我看到一些教程有这个代码,但我不知道如何使用它,为什么它只需要名称?它不需要文件?

    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;
}  

所有的帮助真的很感激!!!请提前感谢。

php ajax image codeigniter upload
1个回答
0
投票

这几乎是从文档逐字逐句:

    $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

我怀疑你的代码没有工作,因为你需要一个合适的(相对)文件路径(你不能使用url);更多关于这里:https://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

© www.soinside.com 2019 - 2024. All rights reserved.