且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

文件上传在codeigniter中不起作用

更新时间:2023-02-06 11:48:44

  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.