Codeigniter - 图像上传适用于localhost(WAMP),但不能在线工作

时间:2017-03-23 18:55:53

标签: php linux apache codeigniter ubuntu

我知道这个问题已被多次回答,但我仍然无法上传图片..

我正在使用codeigniter 2.1.4。 图像上传在localhost上正常工作,但在远程服务器(linux)上不起作用

我已经尝试过这些解决方案: - Codeigniter upload file not working online but works on localhost

但它仍然不起作用。

非常感谢任何帮助

//Controller

function add()
	{
		if(!$this->session->userdata('is_shop_admin')) {
		      $this->check_access('add');
		}
	
		if ($this->input->server('REQUEST_METHOD')=='POST') {			
			$upload_data = $this->uploader->upload($_FILES);
			
			if (!isset($upload_data['error'])) {
					$category_data = array(
					'name' => htmlentities( $this->input->post('name')),
					'ordering' => htmlentities( $this->input->post('ordering')),
					'shop_id' => $this->get_current_shop()->id,
					'is_published' => 1
				);
				
				if ($this->category->save($category_data)) {
					foreach ($upload_data as $upload) {
						$image = array(
							'parent_id'=>$category_data['id'],
							'type' => 'category',
							'description' => "",
							'path' => $upload['file_name'],
							'width'=>$upload['image_width'],
							'height'=>$upload['image_height']
						);
						$this->image->save($image);
					}
								
					$this->session->set_flashdata('success','Category is successfully added.');
				} else {
					$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
				}
				
				redirect(site_url('categories'));
			} else {
				$data['error'] = $upload_data['error'];
			}
		}
		
		$content['content'] = $this->load->view('categories/add',array(),true);		
		$this->load_template($content);
	}

//模型

    function save(&$data, $id=false)
{
    if (!$id && !$this->exists(array('id' => $id, 'shop_id' => $data['shop_id']))) {
        if ($this->db->insert($this->table_name,$data)) {
            $data['id'] = $this->db->insert_id();
            return true;
        }
    } else {
        $this->db->where('id',$id);
        return $this->db->update($this->table_name,$data);
    }       
    return false;
}

//视图

            <?php
            $this->lang->load('ps', 'english');
        ?>
        <ul class="breadcrumb">
            <li><a href="<?php echo site_url() . "/dashboard";?>"><?php echo $this->lang->line('dashboard_label')?></a> <span class="divider"></span></li>
            <li><a href="<?php echo site_url('categories');?>"><?php echo $this->lang->line('cat_list_label')?></a> <span class="divider"></span></li>
            <li><?php echo $this->lang->line('add_new_cat_button')?></li>
        </ul>

        <?php
            $attributes = array('id' => 'category-form','enctype' => 'multipart/form-data');
            echo form_open(site_url('categories/add'), $attributes);
        ?>
            <div class="wrapper wrapper-content animated fadeInRight">
            <legend><?php echo $this->lang->line('cat_info_lable')?></legend>

            <div class="row">
                <div class="col-sm-6">
                        <div class="form-group">
                            <label><?php echo $this->lang->line('category_name_label')?>
                                <a href="#" class="tooltip-ps" data-toggle="tooltip" title="<?php echo $this->lang->line('cat_name_tooltips')?>">
                                    <span class='glyphicon glyphicon-info-sign menu-icon'>
                                </a>
                            </label>
                            <?php echo form_input(array(
                                'name' => 'name',
                                'value' => '',
                                'class' => 'form-control',
                                'placeholder' => 'Category Name',
                                'id' => 'name'
                            )); ?>
                        </div>
                        <div class="form-group">
                            <label><?php echo $this->lang->line('ordering_label')?>
                                <a href="#" class="tooltip-ps" data-toggle="tooltip" title="<?php echo $this->lang->line('cat_ordering_tooltips')?>">
                                    <span class='glyphicon glyphicon-info-sign menu-icon'>
                                </a>
                            </label>
                            <?php echo form_input(array(
                                'name' => 'ordering',
                                'value' => '',
                                'class' => 'form-control',
                                'placeholder' => 'Ordering',
                                'id' => 'ordering'
                            )); ?>
                        </div>

                        <div class="form-group">
                            <label><?php echo $this->lang->line('cat_photo_label')?>
                                <a href="#" class="tooltip-ps" data-toggle="tooltip" title="<?php echo $this->lang->line('cat_photo_tooltips')?>">
                                    <span class='glyphicon glyphicon-info-sign menu-icon'>
                                </a>
                            </label>
                            <input class="btn" type="file" name="images1">
                        </div>  
                </div>

            </div>

            <hr/>
            <button type="submit" class="btn btn-primary"><?php echo $this->lang->line('save_button')?></button>
            <a href="<?php echo site_url('categories');?>" class="btn btn-primary"><?php echo $this->lang->line('cancel_button')?></a>
        </form>
        </div>

        <script>
            $(document).ready(function(){
                $('#category-form').validate({
                    rules:{
                        name:{
                            required: true,
                            minlength: 3,
                            remote: '<?php echo site_url("categories/exists/" . $this->shop->get_current_shop()->id);?>'
                        }
                    },
                    messages:{
                        name:{
                            required: "Please fill Category Name.",
                            minlength: "The length of Category Name must be greater than 4",
                            remote: "Category Name is already existed in the system."
                        }
                    }
                });
            });

            $(function () { $("[data-toggle='tooltip']").tooltip(); });
        </script>

2 个答案:

答案 0 :(得分:0)

我多次遇到过这类问题。 在远程服务器中设置正确的文件夹权限可以解决大多数时间的问题

答案 1 :(得分:0)

这是正常的,可以在localhost中运行,但在服务器中,目录权限非常重要并且会导致一些问题。

要在服务器中工作,您应该检查要上载的目录,并确保该目录具有必要的权限,如777。 您需要调试错误才能找到问题。