【问题标题】:Codeigniter Image Uploading not working onlineCodeigniter 图像上传无法在线工作
【发布时间】:2013-02-23 16:54:57
【问题描述】:

我有一个需要上传照片的应用程序,已在 codeigniter 中完成,可在 localhost 上正常运行,但无法在线运行。

查看页面代码:

<div> 
                  <form id="uploadimage" method="POST" name="uploadimage" action="<?php echo base_url(); ?>onebachhpan/savephoto" enctype="multipart/form-data">
                    <input type="file" name="userfile" id="userfile" accept="image/*" /><br/><br/>
                    <input type="submit" class="button_example" id="upload_button" name="upload_button" value="Upload"/>
             </form>
             <br/>
             <form  id="removeimage" method="POST" name="removeimage" action="<?php echo base_url(); ?>onebachhpan/removephoto" enctype="multipart/form-data">
                   <input type="submit" style="position: absolute; left: 100px; top: 64px; " class="button_example" id="delete_button" name="delete_button" value="Delete"/>
             </form>
             </div>

以下是图片上传的控制器代码: 这在 localhost 上运行良好:

控制器代码:

function savephoto()
    {
         $this->load->library('session');
        $this->load->library('image_lib');
        $config['image_library'] = 'gd2';
        $config['upload_path'] = './profilepic/'; //location to store image
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('userfile'))
        {
            $error = array('error' => $this->upload->display_errors());
         redirect(base_url().'uploaderror');


        }
        else
        {
            $photo = $this->upload->data();
            $data['photo'] = $photo;    
            $id = $this->session->userdata('MemberId');
            $filename = $photo['file_name'];
            $extension = $photo['file_ext'];

            $this->load->model('profile_model');
            $this->profile_model->photo($extension,$id);



          // echo "<pre>";print_r($photo);
            $newpath = './profilepic/'.$id.$extension;

            $config['source_image'] = './profilepic/'.$filename;
            $config['overwrite']=true;

            $config['new_image'] = $newpath;

            $config['width']  = '170';
            $config['height']  = '240';
            $config['maintain_ratio'] = true;


            //$this->load->library('image_lib', $config);

            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            unlink('./profilepic/'.$filename);
            //$this->load->view('onebachhpan_index');
            redirect(base_url().'profile');

        }   
    }

【问题讨论】:

  • 你有什么办法解决这个问题?控制器中 $_FILES 的值是多少?您是否打开了错误报告以查看问题所在?
  • 我正在上传单个文件,因此我不需要 $_FILES,上面的代码在本地主机上也可以正常工作,但是当我将它部署到在线服务器时,照片上传不起作用......您可以查看link
  • 如果上传单个文件或多个文件,数据会发送到PHP的$_FILES变量。 php.net/manual/en/reserved.variables.files.php
  • 设置上传文件夹的权限为777,检查php配置的上传文件的最大大小,并确保在服务器中启用了与本地相同的扩展名等。
  • 您遇到的错误是什么?

标签: codeigniter image-upload


【解决方案1】:

试试这个例子,效果很好

 //File name
$cat_image_name = $_FILES["cat_image"]["name"] ;

$date_added = strtotime(date("m/d/Y H:i:s"));


//File uploading params 
$config['upload_path'] = './uploaded_files/categories';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $date_added."_ipad";
$config['remove_spaces'] = TRUE;

//Loading Library - File Uploading
$this->load->library('upload', $config);

//Upload the image
if (!empty($cat_image_name)) 
{
        $this->upload->do_upload('cat_image');
        $data = array('upload_data' => $this->upload->data());
        $category_image_ipad = $data['upload_data']['file_name'];
        $img_extension = $data['upload_data']['file_ext'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多