Codeigniter文件上传问题

时间:2016-11-24 18:06:00

标签: php codeigniter file-upload codeigniter-3

我尝试使用codeigniter 3上传图片文件,但它无法正常工作。无论我做什么,它总是说"你没有选择要上传的文件"。它在控制台上记录一个错误,当我点击网络选项卡中的错误然后它将我重定向到一个新的选项卡,其中显示错误时,错误500。 这是我的HTML代码:

<div id="container">
<h1>Welcome to CodeIgniter!</h1>

<div id="body">
    <form method="post" enctype="multipart/form-data" action="<?php echo base_url('index.php/welcome/upload'); ?>">
        <input type="text" name="username" value="Zahid Saeed">
        <input type="file" name="profile_img">
        <button type="submit">Submit Form</button>
    </form>
</div>

这是我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper("url");
    }

    public function index()
    {
        $this->load->view('welcome_message');
    }
    public function upload() {  

        $config = array(
            "upload_path" => "./uploads/",
            "allowed_types" => "gif|jpg|png"
        );
        echo "<pre>";
        print_r($this->input->post());
        print_r($_FILES);
        echo "</pre>";

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

        if(!$this->upload->do_upload("profile_img")) {
            echo $this->upload->display_errors();
            echo "IN IF";
        }
        else {
            echo "img uploaded successfully";
        }

    }
}

还有一件事,确切的代码是在linux机器上工作,实际上是在服务器上。但它不能在我的笔记本电脑上工作。我使用的是Windows 8.1

先谢谢

3 个答案:

答案 0 :(得分:6)

may be its php.ini problem

打开php.ini文件

搜索extension = php_fileinfo.dll

如果您使用的是xampp,则默认情况下可以注释

change 
;extension=php_fileinfo.dll

to

extension=php_fileinfo.dll

然后重启你的xampp ......

这解决了我的问题

答案 1 :(得分:0)

使用上传路径的绝对路径可能是个好主意

(+) <$> [2,3,4] <*> [4] = [6,3,4]

当我运行你的代码时,它可以在Windows 10 Home 上的 WAMP64下工作,即文件上传后我会看到

a -> a -> a

答案 2 :(得分:0)

检查文件是否为空

 if(!empty($_FILES['attach_file']['name'])){
      $config['upload_path'] = APPPATH.'../assets/images/';
      $config['allowed_types'] = 'jpg|jpeg|png|mp4';
      $config['file_name'] = 'attach_file_'.time();
      $config['max_size'] = "1024";
      $config['max_height']= "800";
      $config['max_width'] = "1280";
      $config['overwrite'] = false;
      $this->upload->initialize($config);
      if(!($this->upload->do_upload("attach_file"))){
           $data['error']['attach_file'] = $this->upload->display_errors();
      else{
           $coverPhotoData = $this->upload->data(); 
           $coverPhoto =$coverPhotoData['file_name'];
      }
 }elseif($this->input->get('cup')){
      $coverPhoto = $previous['attach_file'];
 }