Meita Putri Aidha Meita Putri Aidha - 2 months ago 13
MySQL Question

upload file and save the path to mysql in codeigniter

i have a field form that implement upload file. how can i make upload file and save the path name to database? i need it because i have to make a download link too. Now, i've success to upload file, but it still saved the name of upload file in database not the path of upload file.
and maybe whether if i make a download link to download file that has uploaded?
thanks. i really appreciate.

this is my controller :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('file');
}

public function index()
{
$this->load->view('v_upload');
}

function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif|jpg|txt|docs|xls';
$config['max_size'] = '2000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';

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

$file1=($_FILES['file1']['name']);
$file2=($_FILES['file2']['name']);
$file3=($_FILES['file3']['name']);


$file = array (
'rab' => $file1,
'kkp' => $file2,
'rcps' => $file3);

foreach ($_FILES as $key => $value) {
if (!empty($value['tmp_name'])) {

if ( ! $this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
//failed display the errors
} else {
//upload success
}

}
}
if ($this->upload->do_upload($key)) {
$this->file->input_data($file,'upload');
}
}
}


this is my view :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<HTML>
<body>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" />
<input type="file" name="file2"/>
<input type="file" name="file3"/>
</div>
<br /><br/>
<input type="submit" value="upload" name="upload" />
</form>
</body>
</HTML>


this is my model :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class File extends CI_Model{

function input_data($data,$table){
$this->db->insert($table,$data);
}
}

Answer

try: $file1=($config['upload_path'].$_FILES['file1']['name']);

for each of the files you upload.

Comments