2009-12-11 41 views
1

我试图创建一个图像上传表单使用CodeIgniter 1.7.2使用CodeIgniter 1.7.2

然而,当我试图上传图片上传无效目录,CI告诉我的上传目录是无效的。

要检查这,我上传一个文件名为test.txt与内容Hello, World!uploads目录,然后我呼应了它的内容,你瞧,它说你好。除此之外,我可以浏览目录并在浏览器中查看它的内容,所以目录肯定存在并且可以查看。

控制器:

class Pictures extends Site_Controller { 
    // snip... 
    public function upload() { 
     $this->load->helper('form'); 
     $this->_render('Do Upload','pictures/upload',array('msg'=>'')); 
    } 
    public function do_upload() { 
     $this->load->helper('form'); 

     $config['upload_path'] = base_url().'uploads/'; 
     // test the directory: 
     // echo file_get_contents($config['upload_path'].'test.txt'); 
     // should echo "Hello, World!" 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '2048'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

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

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

      $this->_render('Do Upload (Errors!)','pictures/upload',$data); 
     }  
     else 
     { 
      $uploaddata = $this->upload->data(); 
      $this->load->model('pictures_Model','pictures'); 
      $this->pictures->insertUploaded($uploaddata,$this->input->post('name'),$this->input->post('caption')); 

      $data['msg'] = 'Successful Upload!'; 
      $this->_render('Do Upload (Success!)','pictures/upload',$data); 
     } 
    } 
    //... 
} 

$this->_render()仅仅是一个快捷方式功能,它包装与所述指定的数据与指定的标题的布局指定的图。

pictures/upload观点:

<h1>Do Upload</h1> 
<? if (isset($msg)) echo $msg; ?> 

<?= form_open_multipart('pictures/do_upload');?> 

<input type="file" name="userfile" /> 
<input type="text" name="name" /> 
<input type="text" name="caption" /> 

<br /><br /> 

<input type="submit" value="Upload" /> 

</form> 

最后,也可能是不必要的,该模型插入:

public function insertUploaded($data,$name,$caption) { 
    $row = array(); 
    $row['filename'] = $data['file_name']; 
    $row['mime'] = $data['file_type']; 
    $row['size'] = $data['file_size']; 
    $row['name'] = $name; 
    $row['caption'] = $caption; 
    $row['img'] = file_get_contents($data['full_path']); 

    $this->db->insert('pictures',$row); 
} 

任何想法,为什么我的上传目录是无效的?

回答

6
$config['upload_path'] = base_url().'uploads/'; 

你应该更改为:

$config['upload_path'] = './uploads/'; 
+0

这是为啥工作? – 2009-12-11 06:48:41

+1

上传路径需要是服务器上的文件路径,而不是URL。 – bradym 2009-12-18 16:25:03

1

CI产生错误时,它是无法找到的目录或即使目录不是可读。首先尝试找出目录是否正常:

echo base_url().'uploads/'; 

如果有,请检查目录权限。 chmod设为755.

如果即使目录权限没有问题,请尝试将下面的行添加到您的上传路径中。

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/' . base_url().'uploads/'; 

请确保您的路径通过回显出现正常。

希望有所帮助。