【问题标题】:Codeigniter Image upload to server folderCodeigniter 图像上传到服务器文件夹
【发布时间】:2019-02-20 15:27:38
【问题描述】:

我是图像上传的新手,想请求有关将文件图像上传到服务器文件夹的帮助。 我正在使用 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.";
    }
}

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

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

非常感谢所有帮助!请提前致谢。

【问题讨论】:

  • 如果文件数组被填充,它不是一个 blob,只是一个普通文件。 Blob 通常来自字符串格式的 post var。您可以使用上传库。如果您想将其保存为 blob,则您的方法是错误的。
  • 感谢指正。我刚刚更正了这个问题。

标签: php ajax image codeigniter upload


【解决方案1】:

这几乎是文档中的逐字记录:

    $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/

【讨论】:

    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多