【问题标题】:Codeigniter Upload function does not workCodeigniter 上传功能不起作用
【发布时间】:2017-12-27 00:09:18
【问题描述】:

我正在使用 codeigniter,在创建其中一个 API 时遇到了问题。我尝试将图像作为文件上传到服务器上,在网上搜索时,我熟悉了 codeigniter 中的内置上传类。请看一下这段代码。我正在使用此tutorial 从 Android 发送文件。

public function upload_image_post(){

 $config['upload_path'] =base_url().'/uploads/';
    $config['file_name'] = rand() .'.jpg';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = 10000;
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = TRUE;
    // $file  = $this->input->post('file');

    $this->load->library('upload', $config);
// $this->upload->initialize($config);
    $file=$_FILES['uploaded_file'];
    // $this->upload->do_upload($file);
    if($file){
      $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'asQ',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is working*/
        $res = $this->db->insert('ww_portfolio_images',$content);     
      }else{
         $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'not file',
        'sp_id'=>'asQaaaaa',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is not working, Thats Obvious*/
        $res = $this->db->insert('ww_portfolio_images',$content);     

      }

      // $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
    if($this->upload->do_upload('uploaded_file')) {
      $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'aaaaaaaaaaaaaaaaas',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is not working*/
        $res = $this->db->insert('ww_portfolio_images',$content);
     $this->response(['result' =>'Success',]  , REST_Controller::HTTP_OK);              
         // return ($arr_image_info['full_path']);
    }
    else{
     $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'asass',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');

        $res = $this->db->insert('ww_portfolio_images',$content);
      /* This is working*/
      $this->response(['result' => 'ERrro']  , REST_Controller::HTTP_OK);    
        // $this->response(['result' =>'Image error',], 433);                
    }
   }

我无法弄清楚我在这里面临的问题。我正在接收一个文件,但它没有上传。

我也尝试使用 $this->upload->do_upload() 代替 $this->upload->do_upload('uploaded_file') 和这个 $config['max_size'] = '10000'; 而不是这个 $config['max_size'] = 10000; 。请帮忙。任何帮助将不胜感激。

此外,当此代码通过 Web 面板运行时,它工作正常。

【问题讨论】:

  • 您是否看到任何错误或警告?

标签: php android codeigniter file-upload image-uploading


【解决方案1】:

如果您提供更多关于您所观察到的错误或警告类型的详细信息,效果会更好。 可能有很多原因。

1) base_url() 为您提供公共 URL。您必须指定上传文件夹的绝对或相对路径。

2)(如果您使用的是 Apache 服务器)您的 Apache 用户没有上传文件夹的写入权限。

3) 文件夹路径不存在。

如果第一个不起作用,请检查其余点。希望对您有所帮助。

问候 穆阿兹

【讨论】:

  • 我没有收到任何错误。它一直给我成功
【解决方案2】:

尝试使用 FCPATH

$config['upload_path'] = FCPATH . '/uploads/';

或者

$config['upload_path'] = './uploads/';

然后http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller

<?php

class Example extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->helper('form');
}

// Name function what every you want remember to change it on view form.
public function upload() {

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

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

if ($this->upload->do_upload('uploaded_file')) {

   // Then the success stuff

   $upload_data = $this->upload->data();

   echo $upload_data['file_name'];

} else {
   // Errors
}

}

}

在视图中我会使用form helper 函数form_open_multipart()

<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>

【讨论】:

  • 如果我选择从 Android 上传,这里的关键是什么
【解决方案3】:
  • 在视图中检查您的表单输入

我的表单视图:

<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
  • 其他可以 上传文件夹的权限问题 如果该文件夹不存在,您必须创建它

并且总是尝试使用日志来调试代码

do_upload 中的文档是我认为输入元素的名称

       if ($_FILES['document']['size'] > 0) {
            $this->load->library('upload');
            $config['upload_path'] = 'uploads/images';
            $config['allowed_types'] = '*';
            $config['max_size'] = $this->allowed_file_size;
            $config['overwrite'] = false;
            $config['encrypt_name'] = true;
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('document')) {
                $error = $this->upload->display_errors();
                $this->session->set_flashdata('error', $error);
                redirect($_SERVER["HTTP_REFERER"]);
            }
            $photo = $this->upload->file_name;
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-03-28
  • 2018-06-02
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2020-03-01
  • 1970-01-01
相关资源
最近更新 更多