CodeIgniter:图片未上传。

时间:2014-04-16 10:17:27

标签: php codeigniter

gallery.php - 查看

 <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <title>CI Gallery</title>
    </head>
    <body>


        <div id="upload">
            <?php
            $this->load->helper("form");
            echo form_open_multipart('gallery/up');
            ?>
            <input type="file" name="file">
            <?php 
            echo form_submit('upload','Upload');
            echo form_close();
            ?>      
        </div>
    </body>
    </html>

gallery.php控制器

<?php
class Gallery extends CI_Controller {

    function index() 
    {   
        $this->load->view('gallery');

    }

    function up() 
    {
            $config['upload_path'] = './images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000000';
            $config['max_width']  = '10240';
            $config['max_height']  = '7680';
            $this->load->library('upload',$config);
            $this->upload->do_upload('file');
            $this->load->view('gallery');
    }


}

这些是我的编码文件。这里没有上传文件。 有人请帮忙。我觉得库上传无法加载,因此它停止工作。如果问题是如何克服这个问题。

4 个答案:

答案 0 :(得分:0)

在html中添加表单标记

<form enctype="multipart/form-data" name="" action=""></form>

答案 1 :(得分:0)

您的代码似乎不错,没有错误。 尝试以下代码检查图像上传是否可能是路径问题? 复制下面的代码并放在上面

"$this->load->view('gallery');" 

这是你控制器的上升功能。

if (! $this->upload->do_upload())
{
  $error = array('error' = $this->upload->display_errors());

 // uploading failed. $error will holds the errors.
}
else
{
  $data = array('upload_data' => $this->upload->data());

 // uploading successfull, now do your further actions
}

答案 2 :(得分:0)

您可以编写上传图像服务器端代码

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());

            print_r($error); //debug it here 

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

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

答案 3 :(得分:0)

问题是要上传图像的文件夹没有写入权限,因此我将该文件夹设为可写,现在工作正常。