在Codeigniter 2.0.2中上传文件的URL重定向问题

时间:2011-10-20 05:31:48

标签: php codeigniter

我正在尝试使用Codeigniter上传图像,遵循CI用户指南 - 但由于网址重定向,我收到错误。

这是我的观点upload_form.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

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

查看2 upload_success.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</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|jpg|png';
        $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);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

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

我在“project_name”文件夹下添加了“uploads”文件夹。

我正在使用此网址上传表单:

  

localhost/project_name/index.php/upload/

提交后,我收到“找不到页面”错误,网址变为:

  

http://localhost/project_name/index.php/upload/localhost/project_name/index.php/upload/do_upload

3 个答案:

答案 0 :(得分:0)

根据提出的意见:

$config['base_url'] = 'http://localhost/codeigniter/';

答案 1 :(得分:0)

$config['base_url'] = 'http://localhost/set root folder name here/';

并使用此变量<?php echo

答案 2 :(得分:0)

更改 $config以外的上传对象的变量名称。 的例如

$this->**config2** =  array(
                          'upload_path'     => "/root/user_name/public_html/*/files",
                          'upload_url'      => "/root/user_name/public_html/*/files",
                          'allowed_types'   => "pdf|doc",
                          'overwrite'       => TRUE
                      );
$this->load->library('upload', $this->config2);
                if(!$this->upload->do_upload())
                {
                     $error = array('error' => $this->upload->display_errors());
                   print_r($error);
                }