CodeIgniter: 图片无法上传。

3

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

<?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个回答

1
在你的HTML中添加表单标签。
<form enctype="multipart/form-data" name="" action=""></form>

1
他有表单标签,是 form_open_multipart 吗? - Albzi
@BeatAlex,是的,但这并不是建设性的。 - SagarPPanchal

1
你可以编写上传图片的服务器端代码。
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);
        }
    } 

0

你的代码看起来很好,没有错误。 尝试使用下面的代码检查图像是否上传,可能是路径问题? 复制下面的代码并将其放在上面

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

在你的控制器的up函数中加入这段代码。

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
}

0
问题在于要上传图像的文件夹没有写入权限,所以我将该文件夹设置为可写,并且现在它可以正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接