variable value in arrray is returned as an integer but needed string in codeigniter
Hello everyone I am completely stuck with this problem.
I want add the name of a file I changed using the string "rename" function in codeigniter to an array and then send that array to a model where I can extract it and add to it my database.
However when I do this the value added in my database for the file name always comes as '1'.
Here is my code, in it I am uploaded and file.
This is part of the controller class with the file name changing and adding to the array
$data = array('upload_data' => $this->upload->data()); // Get the file from the form userfile information
$file = $data['upload_data']['file_name'];
//hacky name generator
$randomstring = random_string('alnum', 4);
$filenew = rename($dir . $file, $dir . $id . '_' . $randomstring . '.jpg'); //basic php rename call, rename my upload now that upload finished
// this array is all the other values from my form fields
$data = array($this->input->post('comment'), $filenew);
$configB['image_library'] = 'gd2'; // this code begins the thumbnail making process, from user guide
$configB['source_image'] = $filenew;//$dir . $id.'.jpg'; // I am using $id for image name, which is my users id, comes from the session
$configB['create_thumb'] = FALSE;
$configB['maintain_ratio'] = TRUE;
开发者_StackOverflow社区 $configB['width'] = 300;
$configB['height'] = 300;
$this->image_lib->initialize($configB);
$this->image_lib->resize();
$this->load->model('membership_model');
// run my model which saves all this to the database, image name also ($filenew)
if($query = $this->membership_model->create_post($id,$data))
{
$data = array('upload_data' => $this->upload->data());
$data['filename'] = $filenew;
$this->load->view('post_success_view', $data);
}
Here is the model
function create_post($id, $data)
{
//get data from array
$content = $data[0];
$filename = $data[1];
// update database to track a new post.
$new_post_insert_data = array(
'content' => $content,
'beer_down' => 0,
'beer_up' => 0,
'user_name' => $this->session->userdata('username'),
'account_id' => $this->session->userdata('userid'),
'file_name' => $filename
);
$insert_post = $this->db->insert('post', $new_post_insert_data);
return $insert_post;
}
Thank you in advanced if you can help me out been stuck here for hours.
rename() changes the name of a system resource, and returns a Bool for success. When you cast that boolean true to a string, you get "1". If you want to store the name in the DB, you should set a variable ie:
$new_name = $dir . $id . '_' . $randomstring . '.jpg';
then call rename:
rename($dir . $file, $new_name);
and use the $new_name to insert in the database.
精彩评论