如何在CodeIgniter中上传图像?

d6kp6zgx  于 2022-12-16  发布在  其他
关注(0)|答案(7)|浏览(179)

在视野中

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

在控制器中

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['overwrite'] = TRUE;
    $config['encrypt_name'] = FALSE;
    $config['remove_spaces'] = TRUE;
    if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('userfile')) {
        echo 'error';
    } else {

        return array('upload_data' => $this->upload->data());
    }
}

我这样调用这个函数

$this->data['data'] = $this->do_upload();

并查看此图像:

<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>


我不知道哪里出错了。

332nm8kg

332nm8kg1#

问题似乎是您将表单请求发送到welcome/do_upload,然后通过$this->do_upload()调用另一个表单中的Welcome::do_upload()方法。
因此,当您在第二个方法中调用$this->do_upload();时,$_FILES数组将为
这就是var_dump($data['upload_data']);返回NULL的原因。
如果要从welcome/second_method上载文件,请将表单请求发送到welcome/second_method,在此调用$this->do_upload();
然后按如下所示更改表单助手功能(在 * 视图 * 中)1:

// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');

使用CodeIgniter上载文件

CodeIgniter**has documented the Uploading process**非常好,通过使用文件上传库。
您可以查看用户指南中的示例代码;此外,为了更好地理解 * 上载配置 *,请查看手册页末尾的 * 配置项说明 * 部分。
还有一些关于CodeIgniter中文件上传的文章/示例,您可能需要考虑:

就像**旁注:**确保在使用CodeIgniter示例代码之前已经加载了urlform帮助函数:

// Load the helper files within the Controller
$this->load->helper('form');
$this->load->helper('url');

// Load the helper files within the application/config/autoload
$autoload['helper'] = array('form', 'url');

1.上传文件时表单必须为“multipart”类型,因此需要使用'form_open_multipart()'帮助函数,该函数返回:``

icomxhvb

icomxhvb2#

在codeigniter中简单上传图像

查找以下代码以轻松上传图像:

public function doupload()
     {
        $upload_path="https://localhost/project/profile"  
        $uid='10'; //creare seperate folder for each user 
        $upPath=upload_path."/".$uid;
        if(!file_exists($upPath)) 
        {
                   mkdir($upPath, 0777, true);
        }
        $config = array(
        'upload_path' => $upPath,
        'allowed_types' => "gif|jpg|png|jpeg",
        'overwrite' => TRUE,
        'max_size' => "2048000", 
        'max_height' => "768",
        'max_width' => "1024"
        );
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('userpic'))
        { 
            $data['imageError'] =  $this->upload->display_errors();
             
        }
        else
        {
            $imageDetailArray = $this->upload->data();
            $image =  $imageDetailArray['file_name'];
        }
        
     }
dnph8jn4

dnph8jn43#

//this is the code you have to use in you controller 

        $config['upload_path'] = './uploads/';  

// directory (http://localhost/codeigniter/index.php/your directory)

        $config['allowed_types'] = 'gif|jpg|png|jpeg';  
//Image type  

        $config['max_size'] = 0;    

 // I have chosen max size no limit 
        $new_name = time() . '-' . $_FILES["txt_file"]['name']; 

//Added time function in image name for no duplicate image 

        $config['file_name'] = $new_name;

//Stored the new name into $config['file_name']

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

        if (!$this->upload->do_upload() && !empty($_FILES['txt_file']['name'])) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('production/create_images', $error);
        } else {
            $upload_data = $this->upload->data();   
        }
vwhgwdsa

vwhgwdsa4#

像这样修改代码。它工作得很好:

public function uploadImageFile() //gallery insert
{ 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $new_image_name = time() . str_replace(str_split(' ()\\/,:*?"<>|'), '', 
    $_FILES['image_file']['name']);
    $config['upload_path'] = 'uploads/gallery/'; 
    $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
    $config['file_name'] = $new_image_name;
    $config['max_size']  = '0';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $config['$min_width'] = '0';
    $config['min_height'] = '0';
    $this->load->library('upload', $config);
    $upload = $this->upload->do_upload('image_file');
    $title=$this->input->post('title');
    $value=array('title'=>$title,'image_name'=>
    $new_image_name,'crop_name'=>$crop_image_name);}
zengzsys

zengzsys5#

$image_folder = APPPATH . "../images/owner_profile/" . $_POST ['mob_no'] [0] . $na;
            if (isset ( $_FILES ['image'] ) && $_FILES ['image'] ['error'] == 0) {
                list ( $a, $b ) = explode ( '.', $_FILES ['image'] ['name'] );
                $b = end ( explode ( '.', $_FILES ['image'] ['name'] ) );
                $up = move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $image_folder . "." . $b );
                $path = ($_POST ['mob_no'] [0] . $na . "." . $b);
nfeuvbwi

nfeuvbwi6#

下面的代码用于一次上传一个文件。这是正确的,完美的上传一个文件。阅读所有注解说明,并按照代码。肯定的是,它是工作。

public function upload_file() {
    ***// Upload folder location***
    $config['upload_path'] = './public/upload/';

    ***// Allowed file type***
    $config['allowed_types'] = 'jpg|jpeg|png|pdf';

    ***// Max size, i will set 2MB***
    $config['max_size'] = '2024';

    $config['max_width'] = '1024';
    $config['max_height'] = '768';

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

    ***// do_upload is the method, to send the particular image and file on that 
       // particular 
       // location that is detail in $config['upload_path']. 
       // In bracks will set name upload, here you need to set input name attribute 
       // value.***

    if($this->upload->do_upload('upload')) {
       $data = $this->upload->data();
       $post['upload'] = $data['file_name'];
    } else {
      $error = array('error' => $this->upload->display_errors());
    }
 }
xxls0lw8

xxls0lw87#

检查$this-〉上载-〉初始化($config);这个对我来说很好用

$new_image_name = "imgName".time() . str_replace(str_split(' ()\\/,:*?"<>|'), '', 
    $_FILES['userfile']['name']);
    $config = array();
    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
    $config['file_name'] = $new_image_name;
    $config['max_size']  = '0';
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|mp4|jpeg';
    $config['file_name'] = url_title("imgsclogo");
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE; 
    $this->upload->initialize($config);
    $this->upload->do_upload();
    $data = $this->upload->data();
}

相关问题