图像上传在codeigniter中无效

时间:2015-03-09 04:30:12

标签: php codeigniter smarty

当我上传图像时,屏幕上显示“失败”,它不会进入成功功能。

  • 相同的代码在我朋友的系统中正常运行
  • 我的配置文件中也进行了更正,然后也无法正常工作

这是我的控制器user.php

<?php
class User extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        session_cache_limiter('private_no_expire');
        session_start();
        $this->load->model('usermodel');
        $this->load->helper(array('form', 'url', 'html', 'date'));
        $this->load->library(array('session', 'javascript', 'form_validation', 'upload','email'));
        $this->load->database();
        $url  = base_url() . 'user';
}

    public function do_upload() 
    {
        $config['upload_path']      = './uploads/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = '5000';
        $config['max_width']        = '1024';
        $config['max_height']       = '768';

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
                $error  = array('error' => $this->upload->display_errors());
                echo "Failed";
        }
        else
        {
                $data   = array('upload_data' => $this->upload->data());
                echo "success";
        }
    }

这是我的profilePicUpload.tpl

<form method="post" action="http://localhost/codeig_smarty/index.php/user/do_upload" enctype="multipart/form-data" />
<input type='file' name='userfile' size='20' />
<input type='submit' name='submit' value='upload'/>

</form>

1 个答案:

答案 0 :(得分:0)

视图     

<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access           allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function file_view(){
$this->load->view('file_view', array('error' => ' ' ));
}
 public function do_upload(){
 $config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'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('file_view', $error);
 }
}
}
?>

查看文件以显示成功消息:upload_success.php

<html>
<head>
<title>Upload File Specification</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
</body>
</html>