【问题标题】:Codeigniter - cannot upload file .pdf in codeigniterCodeigniter - 无法在 codeigniter 中上传文件 .pdf
【发布时间】:2020-02-05 20:28:18
【问题描述】:

我想上传一个 pdf 文件,但如果文件大小超过 3 MB,则无法上传该文件

这是我的配置:

$config = array(
    'upload_path'=>'./assets/file/',
    'allowed_types' => 'pdf',
    'overwrite' => TRUE,
    'max_size' => '0',
    'file_name' => $_FILES['file_peta']['name']
);

【问题讨论】:

  • 上传库配置只是您问题的一部分。即使 Codeigniter 不限制文件大小,您的 PHP 安装和服务器软件(无论是 Apache 还是 Nginx)都有自己的限制。默认情况下,PHP 将上传限制为 2Mb,IIRC,Apache 和 Nginx 也是如此。您还需要处理这些限制,因为有效约束由 3 的最低允许值(您的代码、PHP 和您的服务器指令)决定
  • 这里有一些提示:stackoverflow.com/a/46653262/2275490

标签: php codeigniter pdf upload document


【解决方案1】:

Codeigniter 只是链条中的一环。即使在您的上传库中您没有限制最大文件大小,还有两个地方需要检查:

1.- PHP 的配置指令 2.- Nginx 或 Apache 的配置指令。

每个前者都有一个最大上传大小指令。在您的情况下,真正的限制是三者的下限,因此您需要检查这些配置(或让系统管理员为您完成)并设置适合您的限制

【讨论】:

    【解决方案2】:

    这里是代码 sn-ps 你可以按照它:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        class UploadFileController extends CI_Controller {
            public function __construct() {
                parent::__construct();
                // add these lines
                $this->load->library('upload'); 
                $this->load->helper(array('form', 'url'));
            }
            public function index(){
                $this->load->view('home', array('error' => ' ' ));
            }
            public function do_upload(){
                $config = array(
                    'upload_path' => "./uploads/", // here add directory path where you want to upload and do not forget to give permission
                    'allowed_types' => "gif|jpg|png|jpeg|pdf", // you can add many more types
                    'overwrite' => TRUE,
                    'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                    'max_height' => "768",
                    'max_width' => "1024"
                );
                $this->load->library('upload', $config);
                if($this->upload->do_upload())
                {
                    $data = array('upload_data' => $this->upload->data());
                    $this->load->view('upload_success',$data);
                }
                else
                {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('home', $error);
                }
            }
        }
    ?>
    

    【讨论】:

      【解决方案3】:
      $config['upload_path'] = './uploads/';
      
      $config['allowed_types'] = 'gif|jpg|png';
      
      $config['max_size'] = '100';
      
      $config['max_width'] = '1024';
      
      $config['max_height'] = '768';
      

      定期访问http://zainhub.com/

      【讨论】:

      • 欢迎来到stackoverflow。虽然这可能会回答问题,但如果您要解释原因会更有用。
      猜你喜欢
      • 1970-01-01
      • 2017-01-26
      • 2016-01-30
      • 2012-10-28
      • 1970-01-01
      • 2016-06-25
      • 2012-09-18
      • 2019-03-01
      • 2013-10-04
      相关资源
      最近更新 更多