簡體   English   中英

Codeigniter 3.0,您沒有選擇要上傳的文件

[英]Codeigniter 3.0 , You did not select a file to upload

我已經按照互聯網上與該主題相關的所有答案,經過4個小時的搜索並嘗試仍然沒有成功

拋出的錯誤是您沒有選擇要上載的文件。


編輯

嘗試使用$_FILES方法。


我試過if($this->input->post('userfile')!==null)

檢查圖像是否正在傳遞給控制器​​,事實證明它不是因為沒有執行if條件。

我已按照本指南在服務器上上傳文件。

https://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload

不知道是什么問題。

你可以在上面的鏈接中找到代碼。

我的.htaccess文件

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www\\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] 



編輯:

我嘗試了你們提供的所有建議。

是的,我錯了通過$ _POST而不是$ _FILES檢查圖像

但問題仍然是一樣的。 我發布了我使用的代碼和您可以檢查的URL。

真的很感激任何幫助。

ImageUploadController.php

 <?php class ImageUploadController extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } public function index() { $this->load->view('image-upload/upload_form', array('error' => ' ' )); } public function store($workType) { $config['upload_path'] = './assets/'; $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('imageFile')) { if(isset($_FILES['imageFile'])) echo "isset"; $error = array('error' => $this->upload->display_errors()); $this->load->view('image-upload/upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('image-upload/upload_success', $data); } } } ?> 

upload_form.php

https://gist.github.com/VickySalunkhe/1dc66ec6d3af8cf097a5c01b5009c804

upload_result.php

https://gist.github.com/VickySalunkhe/691626e4fe6dd8a8b3896d210e4b22b9

為請求設置的路由

 $route['upload'] = 'ImageUploadController'; //stores the images on server $route['store/(:any)'] = 'ImageUploadController/store/$1'; 

你可以忽略第二條路線的部分,它可以用於其他一些將在稍后實施的邏輯部分。

要在CodeIgniter 3中上傳文件,我建議您使用File Uploading類。

您可以在此處找到鏈接: 在CodeIgniter中上傳文件

就我所看到的,就是你上面使用的。

文檔說您必須首先加載上傳庫,並使用一些配置設置(請參閱文檔):

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

然后執行實際上傳的方法(基於config params)是這樣的:

$this->upload->do_upload('<your-file-name>');

請注意,它必須使用您通過HTML上傳的文件的名稱。 每次使用HTML <input type="file">元素上傳文件時,在POST數組上都找不到文件本身,但在FILES數組中。

試試Blow Code

1)HTML:

<form action="/profile_upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="Submit">
</form>

2)PHP文件:

if($_FILES['file']['name']!='')  {
    $config['upload_path'] = './uploads/admin/big';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '10240';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('file')) {
        // ERROR
        $error = array('error' => $this->upload->display_errors());
    } 
    else {
        // SUCCESS
        $file_name = $this->upload->file_name;
    }
}
  1. 檢查表單enctype屬性,例如

  2. 檢查$ _FILES變量是否為空。

視圖:

<?php echo form_open_multipart('updateFunctionName');?>

<div class="col-md-6">
  <div class="form-group">
    <label for="artworkpath">Artwork</label>
    <input type="file" class="form-control" id="artworkpath" name="artworkpath" value="<?php echo $artworkpath; ?>">
  </div>
</div>

</form>

控制器:

function updateFunctionName(){
    $artworkpath = $this->input->post('artworkpath');

    $CheckFileName = true;
    if ($_FILES['artworkpath']['size'] == 0 && $_FILES['artworkpath']['error'] == 0)
    {
    $CheckFileName = false;
    }
    else
    {
    $filename2 = 'Artwork_'.rand(0,999999);

    if( ! is_dir('uploads') ){mkdir('uploads',0777,TRUE); };      
    $path='uploads/';        
    $imgtmpname=$_FILES['artworkpath']['tmp_name'];
    $name = $_FILES["artworkpath"]["name"];
    $ext = end((explode(".", $name)));
    $fullpath= $path .$filename2.'.'.$ext;
    $filename = $filename2;
    move_uploaded_file($imgtmpname,$fullpath);
    }

在DB中將此變量數據保存在列中:

    $Info = array();
    if ($ext <> '')
    {
    $Info = array('artworkpath'=>$fullpath, //other fields);
    }
    else
    {
    $Info = array(//other fields);
    }
    $this->user_model->updateRecord($Info, $Id);
}

模型:

function updateRecord($Info, $Id)
{
    $this->db->where('Id', $Id);
    $this->db->update('tablename', $Info);
}

暫無
暫無

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

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