1

i have try and try and i just cant get my image to upload. And i cant grab my image name when i try to echo it out :S.

can you see what im doing wrong.

here is my controller:

<?php 
//ADMIN PAGE
if (! defined('BASEPATH')) exit('No direct script access');

class News extends CI_Controller {


    //Write post when logged in as admin
    function write()
    {

        //insert image
        $config['upload_path'] = APPPATH .'/archive/img/news/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size']         = '9000';
        $config['encrypt_name']     = true;

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

        $file_data = $this->upload->data();

        $newsData = array(
            'headline'      => $this->input->post('headline'),
            'description'   => $this->input->post('description'),
            'content'       => $this->input->post('content'),
            'creater'       => $this->session->userdata('username'),
            'ip'            => $this->session->userdata('ip'),
            'imgPath'       => $file_data['file_name']
        );


        echo "<pre>";
        //print_r( $this->upload->data());
        //print_r($file_data);
        //print_r($_FILES);
        //print_r($this->input->post());
        print_r($newsData);
        echo "</pre>";


        $this->load->model('admin/news_model');
        $this->news_model->insertNews($newsData);


        $data['main_content'] = 'admin/write_view';
        $this->load->view('template', $data);

    }

}

And my view file where i upload my image

<div id="inputStyle">


<?php

echo form_open_multipart('admin/news/write');

echo form_input('headline', 'overskrift');

echo form_upload('newsImage');

echo form_textarea('description', 'indhold');

echo form_textarea('content', 'content');

echo form_submit('create', 'Opret nyhed');

echo form_close();


?>




</div><!-- inputStyle -->
4
  • check if there was any error with $this->upload->display_errors() before you try to grab the filename Commented Dec 27, 2011 at 11:04
  • @Kokers i got this error: The upload path does not appear to be valid. how do i get the full path? my folder is not in the application folder but in the root folder Commented Dec 27, 2011 at 12:13
  • make sure the destination folder is writable (775 or 777) Commented Dec 27, 2011 at 14:04
  • remove APPPATH from config['upload_path']... If I understood you right, this should work for you. Commented Dec 28, 2011 at 6:33

1 Answer 1

4

I have edited your code. It may be work for you. If your folder is in root folder of application then no need to use APPPATH. I have also edited this in your code. Try this.

//Write post when logged in as admin
function write()
{
    //insert image
    $config['upload_path'] = 'archive/img/news/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size']         = '9000';
    $config['encrypt_name']     = true;

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

    $file_data = $this->upload->data();

    $newsData = array(
        'headline'      => $this->input->post('headline'),
        'description'   => $this->input->post('description'),
        'content'       => $this->input->post('content'),
        'creater'       => $this->session->userdata('username'),
        'ip'            => $this->session->userdata('ip'),
        'imgPath'       => $_FILES['newsImage']['name']
    );


    echo "<pre>";
    //print_r( $this->upload->data());
    //print_r($file_data);
    //print_r($_FILES);
    //print_r($this->input->post());
    print_r($newsData);
    echo "</pre>";


    $this->load->model('admin/news_model');
    $this->news_model->insertNews($newsData);


    $data['main_content'] = 'admin/write_view';
    $this->load->view('template', $data);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Damn thanks :), but if i want to grab the encrypted name how do i do that?
I don't know why you want to grab encrypted name?
because i want to store the name in the database so i can show to right news image to my news post :)
you can store real name of image in database.
but on my folder the image name is the crypted name :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.