CodeIgniter:未上载图像


CodeIgniter: Image is not uploading.

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');
    }

}

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

在html 中添加表单标签

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

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

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

您的代码看起来不错,没有错误。试试下面的代码来检查图片上传与否可能是路径问题?复制下面的代码并放在上面

"$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
}

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