簡體   English   中英

Codeigniter文件上傳代碼不起作用

[英]Codeigniter file upload code not working

控制器代碼:

if(isset($_FILES['imageupload']) && $_FILES['imageupload']['size'] > 0){
    echo 'inside if';
    if ( ! $this->upload->do_upload('imageupload'))
    {
    echo 'inside if';
    $error = array('error' => $this->upload->display_errors());
    }
    else
    {
    echo 'inside else';
    $upload_data = $this->upload->data(); 
    $file_name[] = $upload_data['file_name'];
    } 
}

查看代碼:

<input type="file" class="default" id="imageupload" name="imageupload">

錯誤:在非對象上調用成員函數do_upload()

我可以解決此錯誤,請幫助我。

您是否將庫上傳文件加載到控制器上? 我想您的代碼的問題是缺少此代碼$this->load->library('upload', $config); 嘗試查看此文檔,希望對您有所幫助。 https://www.codeigniter.com/userguide3/libraries/file_uploading.html

該錯誤表明未加載upload庫。 您需要將上傳庫加載到某個地方,並且在控制器的構造函數中是一種可能的選擇。 將以下內容添加到控制器代碼中。

function __construct()
{
  parent::__construct();
  $this->load->library('upload');
}

另一個加載該庫的地方是使用$autoload['libraries']項目在application / config / autoload.php中

$autoload['libraries'] = array('upload');

可以同時加載其他庫。 有關更多詳細信息,請參見autoload.php中的注釋。

添加上載庫之前,您可以使用config調用do_upload

$config['upload_path'] = '/path/';
$config['allowed_types'] = '*';
$config['file_name'] = 'file_name';
$config['overwrite'] = TRUE|FALSE;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM