Upload file to database using codeigniter -
i need upload file in mysql database using codeigniter. view is:
<?php echo form_open_multipart('home/addpagina2'); ?> <label for="titolo">titolo:</label> <input type="text" size="20" id="titolo" name="titolo"/> <br/> <label for="testo">testo</label><br/> <textarea name="testo" cols="50" rows="10"></textarea> <br/> <label for="file">file:</label> <input type="file" name="file" /> <br /> <input type="submit" value="aggiungi"/> </form> and controller is:
function addpagina2(){ $this->form_validation->set_rules('titolo', 'titolo', 'trim'); $this->form_validation->set_rules('testo', 'testo', 'trim'); if($_files["file"]["size"] > 0){ $tmpname = $_files["file"]['tmp_name']; $fp = fopen($tmpname, 'r'); $file = fread($fp, filesize($tmpname)); $file = addslashes($file); fclose($fp); } $pagina = array(); $pagina['titolo'] = $this->input->post('titolo'); $pagina['testo'] = $this->input->post('testo'); $pagina['file'] = $file; $this->db->insert('pagine', $pagina); redirect('home/pagine'); } when want display file call view:
<?php header("content-type: ".$file['type']); echo $file; ?> that called controller:
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class file extends ci_controller { function __construct(){ parent::__construct(); } public function index(){ } function id($id){ $immagine = $this->db->get_where('pagine', array('id' => $id)); $data['file'] = $immagine->row()->file; $this->load->view('file', $data); } }
the problem have error while call view in browser, can see here: http://mattialori.net/amv/index.php/file/id/2 if upload file on database using phpmyadmin works.
how can do?
why don't save file name db? can upload img in specific folder , in view yuo can set full path folder , append file name @ end.
edit: can use file upload class in way:
//check if file exists in form if($_files['file']['name']!=""){ //load library $this->load->library('upload'); //set config $config['upload_path'] = './uploads/'; //use relative or absolute path $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['overwrite'] = false; //if file exists saved progressive number appended //initialize $this->upload->initialize($config); //upload file if( ! $this->upload->do_upload("file")){ //echo errors echo $this->upload->display_errors(); } //if upload success $file_name = $this->upload->file_name; //save file name db }
Comments
Post a Comment