Welcome Guest, Not a member yet? Register   Sign In
How to store image [full_path] in database
#1

[eluser]Dchen05[/eluser]
I know this is a simple question and I have read the user guide on this subject. But I cannot figure out how to take the [full_path] from the array called upload_data and store it in a field called image_path in my database. All the text fields work fine in my form and get stored in the DB properly. What do I have to write in order to get just [full_path] into the database?

Thanks guys!

Here is the controller:

Code:
function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    
        
        $this->load->library('upload', $config);
    
    
        $this->db->insert('characters', $_POST);
    
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            $this->load->view('upload_form', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            
            $this->load->view('upload_success', $data);
        }
        
    }

And the view:

Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
    
<h1>Upload a New Character</h1>

&lt;?php echo $error;?&gt;

&lt;?php echo form_open_multipart('upload/do_upload');?&gt;

<p>Image:&lt;input type="file" name="userfile" size="20" /&gt;&lt;/p>



<br /><br />

    
    
<p>Character Bio&lt;textarea name="bio" rows="10"&gt;&lt;/textarea></p>
<p>Character Name&lt;input type="text" name="char_name" /&gt;&lt;/p>
&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]ciGR[/eluser]
Hi,
You get the info that you want from the table data
Code:
$data = array('upload_data' => $this->upload->data());
Look at the bottom of this page http://ellislab.com/codeigniter/user-gui...ading.html , and you can see the available keys-values of the data array.
For example you can get
full_path The absolute server path including the file name

or if you save the images in a concrete path, you can save only the filename in the database, that you can get after success upload from $data['file_name']
#3

[eluser]Dchen05[/eluser]
Thanks for the response,

My question is how do I get $data['file_name] into the DB. The other fields automatically went into the correct DB fields because they had the same name and were text I think.

Can I set a variable something like this?

$data["full_path"] = $filepath;

$filepath->db->insert('characters', $_POST["filepath"]);
#4

[eluser]ciGR[/eluser]
For example, you have a two fields in the database table title and image_path

$filepath = $data['full_path'];(or you can save only the image name with $data['file_name'];
$data = array(
'title' => 'The title for this image' ,
'image_path' => $filepath
);

$this->db->insert('your_table', $data);

You save in the database the path(the path it's also text or better you can set it as varchar)
#5

[eluser]Dchen05[/eluser]
Thanks so much for the help. Unfortunately I'm still getting an error.
Do I have to declare $filepath as a variable before?


A PHP Error was encountered

Severity: Notice

Message: Undefined index: full_path

Filename: controllers/upload.php

Line Number: 44




Code:
function do_upload()
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    
        
        $this->load->library('upload', $config);
    
    
        $this->db->insert('characters', $_POST);
        
        
    
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            $this->load->view('upload_form', $error);
        }    
        else
        {
            
            $data = array('upload_data' => $this->upload->data());
            
            $filepath = $data['full_path'];
            $data = array(
              'image_path' =>  $filepath
                );

            $this->db->insert('characters', $data);
            
            
            
            $this->load->view('upload_success', $data);
            
            
        }
#6

[eluser]cahva[/eluser]
You are assigning $this->upload->data() to $data['upload_data'] so thats why it couldnt find $data['full_path'] (so it would be found in $data['upload_data']['full_path'] ). Try:
Code:
$data = $this->upload->data();

$filepath = $data['full_path'];

$data = array(
    'image_path' => $filepath
);

$this->db->insert('characters', $data)
...
#7

[eluser]ciGR[/eluser]
Yes cahva has right, that's the way I use it, but in my first post I copy-paste from you post(and in user guide is with the same way) and post it.
#8

[eluser]minagabriel[/eluser]
i usually do this
at the controller :
i save the file name string to a variable
$x= $data['upload_file]['file_name'];
and pass this variable to the model
$this->load->model(any_model)
$this->any_model->any_function($x)

and ion the model : when inserting data
$this->db->insert('any_table' , array('image_name'=>post_any ,
'image_path'=>$x));
#9

[eluser]adityajoshi[/eluser]
how do i get the file location of thumbs i mean resize image which i have in thumbs folder




Theme © iAndrew 2016 - Forum software by © MyBB