【问题标题】:Invalid upload directory with CodeIgniter 1.7.2CodeIgniter 1.7.2 的上传目录无效
【发布时间】:2009-12-11 05:46:18
【问题描述】:

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

但是,当我尝试上传图像时,CI 会通知我上传目录无效。

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

控制器:

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

知道为什么我的上传目录无效吗?

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:
     $config['upload_path'] = base_url().'uploads/';
    

    您应该将其更改为:

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

    【讨论】:

    • 上传路径必须是你服务器上的文件路径,而不是URL。
    【解决方案2】:

    当 CI 无法找到目录或目录不可读时,它会生成该错误。首先尝试找出目录是否正常,例如:

    echo base_url().'uploads/';
    

    如果有,请检查目录权限。 chmod 到 755。

    如果甚至目录权限都可以,请尝试将以下行添加到您的上传路径。

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

    通过回显来确保您的路径顺利。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多