简体   繁体   English

CodeIgniter 1.7.2的上传目录无效

[英]Invalid upload directory with CodeIgniter 1.7.2

I am attempting to create an image-upload form with CodeIgniter 1.7.2 我正在尝试使用CodeIgniter 1.7.2创建图像上传表单

However, when I attempt to upload an image, CI informs me the upload directory is invalid. 但是,当我尝试上传图像时,CI会通知我上传目录无效。

To check this, I uploaded a file to the uploads directory called test.txt with the content Hello, World! 为了检查这一点,我将一个文件上传到名为test.txtuploads目录中,其内容为Hello, World! I then echoed out the contents of it, and lo and behold, it says hello. 然后我回显了其中的内容,瞧,它问好了。 Beyond that, I can browse to the directory and view the contents of it in my browser, so the directory definitely exists and is viewable. 除此之外,我还可以浏览目录并在浏览器中查看目录的内容,因此目录肯定存在并且可以查看。

Controller: 控制器:

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() is just a shortcut function that wraps the specified view with the specified data in a layout with a specified title. $this->_render()只是一个快捷功能,它将具有指定数据的指定视图包装在具有指定标题的布局中。

pictures/upload view: 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>

Finally, and probably unnecessary, the model insert: 最后,并且可能是不必要的,模型插入:

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

Any idea why my upload directory is invalid? 知道为什么我的上传目录无效吗?

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

您应该将其更改为:

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

CI generates that error when it is not able to find the directory or even when directory is not readable. 当无法找到目录时,甚至目录不可读时,CI都会生成该错误。 First try to find out that the directory comes fine like: 首先尝试找出目录是否正常,例如:

echo base_url().'uploads/';

If it comes, check with directory permissions. 如果出现,请检查目录权限。 chmod to 755. chmod为755。

If even directory permission is fine, try to add below line to your upload path. 如果即使目录许可也可以,请尝试在您的上传路径中添加以下行。

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

Make sure that your path is coming fine by echoing it out. 通过回显路径,确保路径正常。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM