首页 文章

无法在代码点火器中上传文件 .

提问于
浏览
1

我正在尝试使用代码点火器上传图像,但它没有显示任何失败消息的成功 . 请帮忙 .

This is a file upload form
view- Upload_form.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="">

            <?php echo $error; ?>

            <?php echo form_open_multipart('upload/do_upload');?>
            <input type="file" name="userfile" size="20" />
            <br><br>

            <input type="submit" value="upload"/>

        </form>

    </body>
</html>

This is a view success file
view - upload_success.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Upload form</title>
    </head>
    <body>
        <h1> YOUR FILE WAS SUCCESSFULLY UPLOADED </h1>
        <ul>
  <?php foreach ($upload_data  as $item => $value); ?>          

            <li><?php echo $item; ?> : <?php echo $value; ?></li>
            <?phpendforeach ;?>

        </ul>

   <?php echo anchor('upload','upload Another file'); ?>     
    </body>
</html>

This is a controller file.
controller - upload.php

<?php

class Upload extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper (array('form','url'));
    }

    public function index(){
        $this->load->view('Upload_form',array('error'=>' '));

    }

    public function do_upload(){

       $config = array(  
              'upload_path' => './uploads/',
           'allowed_types' => 'gif|jpg|png',
           'max_size' => 100,
           'max_width' => 1024, //Mainly goes with images only
          'max_heigth' => 768
        );


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

    if(!$this->upload->do_upload('userfile'))    {

        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form',$error);
    }
    else{
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success',$data);

    }


    }

}

我在代码点火器的根目录下创建了uploads文件夹 .

2 回答

  • 0

    请对您的代码 controllerview page 进行一些更改 . 还要确保在您的视图 Upload_form 中使用放置在 application folder.below代码之外的 uploads 文件夹

    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <!--<form action="" method="post">-->
    
            <?php echo $error; ?>
    
            <?php echo form_open_multipart('upload/do_upload');?>
            <input type="file" name="userfile" size="20" />
            <br><br>
    
            <input type="submit" value="upload"/>
    
       <?php 
       form_close();
       ?>
    
    </body>
    

    并尝试在控制器中执行以下代码 upload

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Upload extends CI_Controller {
           function __construct()
            {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
    
            }
            public function index(){
            $this->load->view('upload_form',array('error'=>' '));
    
        }
    
        public function do_upload(){
    
           $config = array(  
                  'upload_path' => './uploads/',
               'allowed_types' => 'gif|jpg|png',
               'max_size' => 100,
               'max_width' => 1024, //Mainly goes with images only
              'max_heigth' => 768
            );
    
    
            $this->load->library('upload',$config);
    
        if(!$this->upload->do_upload('userfile'))    {
    
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form',$error);
        }
        else{
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('upload_success',$data);
    
        }
    
    
        }
    
    }
    
  • 0

    你的upload_path可能不正确,所以这里是我正在使用的代码:

    1.在控制器顶部添加这个以声明upload_path:

    function __construct()
    {   
      parent::__construct();
      $this->uploadPath = realpath(APPPATH . '../uploads');
    }
    

    2.更改$ config upload_path

    './uploads/'

    $ this-> uploadPath


    有关详细信息,请参阅我的uploadProfile控制器:

    function upload_profilePic($userID)
        {
            //create a unique ID
            $filename = $userID. '-' .uniqid();
    
            //config upload
                $config=array(
                    'allowed_types' =>  'jpg|jpeg|png|PNG',
                    'upload_path'   =>  $this->imagePath,
                    'max_size'  =>  90000,
                    'file_name' =>  $filename
                );
            //upload
                $this->load->library('upload',$config);
                $this->upload->do_upload('profilePic');
    
            //get file data
                $data = $this->upload->data();
                $fileExtension = $data['file_ext'];
    
            if($fileExtension=="")
                $filename = '';
            else
                $filename=$data['file_name'];
    
            //
            $data = array(
                'profilePic' => $filename,
            );
    
            $this->db->where('userID',$userID)
            ->update('tblusers',$data);
        }
    

相关问题