【问题标题】:Codeigniter 3.0 , You did not select a file to uploadCodeigniter 3.0,您没有选择要上传的文件
【发布时间】:2019-06-22 20:32:03
【问题描述】:

我已经关注了互联网上与该主题相关的所有答案,经过 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';

您可以忽略第二条路线的部分,它用于稍后将实现的其他逻辑部分。

【问题讨论】:

  • 文件存储在$_FILES 数组中,而不是$this-&gt;input-&gt;post() 访问的$_POST 数组中。我很难相信您遵循了您列出的文档并提出了这个解决方案,而不是我否认有问题。为什么不将视图/模型/控制器的相关部分发布到最后一次尝试,以便我们可以看到哪里出错了?确保您的表单也是多部分表单!
  • 嗨@Alex我尝试使用 $FILES 方法仍然无法正常工作,我已经编辑了我的问题 [包括我正在使用的代码] 请通过它。谢谢你
  • 您发布到store/user 而不是ImageUploadController/store 您是否设置了路线?还是这是一个错误?
  • 我已经为它设置了一条路线。我现在也在我的问题中添加了我的路线。
  • 在你的store函数的最开始你能给我一个print_r($_FILES); exit;

标签: php .htaccess codeigniter file-upload routes


【解决方案1】:

要在 CodeIgniter 3 中上传文件,我建议您使用 File Uploading 类。

您将在此处找到链接:Uploading files in CodeIgniter

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

文档说您必须先加载上传库,并进行一些配置设置(请参阅文档):

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

然后实际上传的方法(基于配置参数)是这样的:

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

注意它说的地方,你必须使用你通过 HTML 上传的文件的名称。文件本身不在 POST 数组中,但在 FILES 数组中,每次您使用 HTML &lt;input type="file"&gt; 元素上传文件时。

【讨论】:

  • 嗨 @MarkSkayff 我已经在使用 CodeIgniter 帖子中提到的确切方法。问题还是一样,我已经编辑了我的问题,所以你可以看看我正在使用的代码。
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    1. 检查表单 enctype 属性,例如

    2. 检查 $_FILES 变量是否为空。

    【讨论】:

      【解决方案4】:

      查看:

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

      在数据库中将此变量数据保存在列中:

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

      【讨论】:

        猜你喜欢
        • 2014-03-05
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 2013-07-26
        • 2016-03-13
        相关资源
        最近更新 更多