Reputation: 1697
i am trying to rename an image file and save the file location in database. i am having problem problem with it, as i want to name the file as the id of the table row i am inserting in. after uploading and inserting i want to see my table as like this:
----------------------------------------
| id | name | category | image |
----------------------------------------
| 1 | foo | category | uploads/1.jpg |
----------------------------------------
the id field is auto incremented. here is my code for it:
function service()
{
$con=$this->do_upload();
$id=mysql_insert_id();
$data=array(
'name'=>$this->input->post('name'),
'category'=>$this->input->post('category'),
'image'=>'uploads/'.$id.$con['file_ext'];
);
$query=$this->db->insert('table',$data);
return $query;
}
this is not the correct way and as assumed the file is not saved in the database as wanted.instead of saving it as 'uploads/1.jpg' it is saved as 'uploads/0.jpg'.
can any one provide me the right convention to do it?
n.b. i am using codeigniter 2.1.0 and mySQL database.
Upvotes: 1
Views: 3569
Reputation: 1
I have built in the same functionality in one of my apps. I am using a uuid generator class which I downloaded from here: https://github.com/Repox/codeigniter-uuid.
Using this class I am generating uuid's for newly uploaded images and then set the filename to ".ext". I am storing the uuid in a database field as well for the user who has uploaded the image to have a reference to the file name.
It's working pretty well for me.
So a code snippet looks like this:
$this->load->library('uuid');
$uuid = $this->uuid->v4();
$new_file_name = $uuid.$image_data['file_ext'];
Hope this helps.
Best regards Sebastian
Upvotes: 0
Reputation: 10634
function service()
{
$con=$this->do_upload();
$id = time();
$data=array(
'name'=>$this->input->post('name'),
'category'=>$this->input->post('category'),
'image'=>'uploads/'.$id.$con['file_ext'];
);
$query=$this->db->insert('table',$data);
return $query;
}
using time()
instead of mysql_insert_id.. then you just need to do the same thing in the do_upload()
function.. where it asks for filename just use time()
.
Upvotes: 1
Reputation: 152226
You cannot access id with mysql_insert_id
without calling mysql query before. You have to insert new record and then update it. with new filename.
Edit:
When I was uploading images/other files to the server, I was always renaming them with some random hash. So files had unique hash name on filesystem and in mysql row there was set just this hash to associate db row with file.
So before inserting new record into db, generate some random string with checking if file with this name already exists:
$filename = '';
do {
$filename = substr(md5(uniqid(rand(), true)), 0, 8);
} while ( !file_exists('uploads/'.$filename) );
Do it in do_upload
method and let it to return file's name in $con
array.
Upvotes: 5
Reputation: 30051
If you just use a unique value for your id
field you could do like this:
function update_service($id)
{
$con=$this->do_upload();
$result = mysql_query("SELECT MAX(id) FROM <table_name>");
$data = mysql_fetch_row($result);
$id = $data[0] + 1;
$data=array(
'name'=>$this->input->post('name'),
'category'=>$this->input->post('category'),
'image'=>'uploads/'.$id.$con['file_ext'];
);
$query=$this->db->insert('table',$data);
return $query;
}
Upvotes: 1