文件上传在codeigniter

时间:2015-07-08 08:25:58

标签: php codeigniter file-upload

我是codeigniter的初学者。自从过去两天以来,我一直在尝试使用CI指南构建文件上传系统。问题是图像没有上传到上传目录 ./ uploads / 我怀疑问题在于上传文件夹的权限,但我不确定。我在Windows 7上使用XAMPP在本地构建应用程序。按下上传按钮后,没有任何反应。 (它应该显示显示错误或重定向到成功页面。)

查看 - upload_form.php    `     

    <title>Upload Form</title>
</head>

<body>
<form>

<?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>

成功页面 - upload_success.php

<html>

<head>
<title>Upload Form</title>
</head>


<body>

<h3>Your file was uploaded successfully</h3>

<ul>

<?php foreach ($upload_data as $item => $value): ?>
<li><?php echo $item; ?>: <?php echo $value; ?></li>
<?php endforeach; ?>

</ul>

<p> <?php echo anchor('upload' , 'Upload Another File!'); ?> </p>

</body>
</html>

controller - upload.php

<?php

class Upload extends CI_Controller{


function __construct()
{

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

function index(){

    $this->load->view('upload_form' , array('error' => ''));
}


function do_upload()
{


    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|png|jpg|jpeg' ;
    /*$config['max_size'] = '100';
    $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());

    $this->load->view('upload_form' , $error); //show error page
}

else
{


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

    $this->load->view('upload_success' , $data);
}
}

}

?>

路由 - routes.php

$route['default_controller'] = 'welcome';
$route['404_override'] = "";

$route['(:any)'] = "upload";

1 个答案:

答案 0 :(得分:1)

 View- upload_form.php    
 <form> //remove these thing, form_open_multipart('upload/do_upload') will do the job. If you are giving both, then one form tag is unable to find it's closing tag and action path;

 In controller, make sure $config['upload_path'] = './uploads/'; path is writable and it exist.

Rest代码看起来很好且可行。