文件上传在codeigniter中不起作用 [英] File upload not working in codeigniter
问题描述
View- upload_form.php
`
< 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>上传表格< /标题>
< / head>
< body>
< h3>您的档案已成功上载< / h3>
< ul>
<?php foreach($ upload_data as $ item => $ value):?>
< li><?php echo $ item; ?>:<?php echo $ value; ?>< /锂>
<?php endforeach; ?>
< / ul>
< p> <?php echo anchor('upload','Upload Another File!'); ?> < / p为H.
< / body>
< / html>
控制器 - upload.php
<$ p $ (
$
parent :: __ construct();
$ this-> load-> helper(array('form','url'));
函数index(){
$ this-> load-> view('upload_form',array('error'=>' ));
函数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->加载 - >库('upload',$ config);
如果(!$ this-> upload-> do_upload())
{
$ error = array('error'= > $ this-> upload-> display_errors());
$ this-> load-> view('upload_form',$ error); //显示错误页面
$ b $ else
$ b $ data = array('upload_data'=> $ this- > upload->数据());
$ this-> load-> view('upload_success',$ data);
}
}
}
?>
路由 - routes.php
$ route ['default_controller'] ='welcome';
$ route ['404_override'] =;
$ route ['(:any)'] =上传;
upload_form.php
< form> //删除这些东西,form_open_multipart('upload / do_upload')将完成这项工作。如果您同时提供,则一个表单标记无法找到它的结束标记和操作路径;
在控制器中,确保$ config ['upload_path'] ='./uploads/';路径是可写的并且存在。
剩下的代码看上去很好,可行。
i am a beginner at codeigniter. since past 2 days i've been trying to build a file upload system using CI's guide. The problem is that the images are not getting uploaded in the upload directory ./uploads/ I suspect that the problem lies with the permissions of the upload folder but i am not sure. I am building the application locally using XAMPP on Windows 7. after pushing the upload button, nothing happens. (it is supposed to either show display errors or redirect to a success page.)
View- 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>
success page - 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);
}
}
}
?>
routing - routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = "";
$route['(:any)'] = "upload";
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 code is looking good and workable.
这篇关于文件上传在codeigniter中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!