Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:56:49+00:00 2026-06-05T01:56:49+00:00

I need to upload a file in a mysql database using codeigniter. My view

  • 0

I need to upload a file in a mysql database using codeigniter.
My 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 I want to display the file I call this view:

<?php
header("Content-type: ".$file['type']);
echo $file;
?>

that is called by this 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 is that I have an error while i call the view in the browser, as you can see here: http://mattialori.net/amv/index.php/file/id/2
If I upload a file on the database using phpmyadmin it works.

How can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:56:51+00:00Added an answer on June 5, 2026 at 1:56 am

    Why don’t you just save the file name into the db?
    Then you can upload the img in a specific folder and in your view yuo can set the full path from your folder and append the file name at the end.

    EDIT:
    you can use the File Upload class in this way:

    //Check if the file exists in the form
    if($_FILES['file']['name']!=""){
    
    //load library
    $this->load->library('upload');
    
    //Set the 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 the file exists it will be saved with a progressive number appended
    
    //Initialize
    $this->upload->initialize($config);
    
    //Upload file
    if( ! $this->upload->do_upload("file")){
    
        //echo the errors
        echo $this->upload->display_errors();
    }
    
    
    //If the upload success
    $file_name = $this->upload->file_name;
    
    //Save the file name into the db
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to upload file the server via .php file, and i've got this
I need to upload a file in C# using an httpwebrequest. I don't need
I need to upload .dat file from jsp page. I am using struts. I
how to upload a file into varbinary using ASP.NET/C# upload button. I need the
I have to import data from a CSV file into MySQL database using JSP/Servlet.
im really having a tough time trying to upload images using php and mysql,
i need to upload file without using a form. i need to do this
I need to upload a file from the client to an asp.net page. The
I'm writing GWT application where I need to upload video file and encode that
Inside a grails application, I need to upload a file under web-app/js, add a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.