[英]variable value in arrray is returned as an integer but needed string in codeigniter
大家好,我完全陷入了這個問題。
我想使用codeigniter中的字符串“rename”函數將我更改的文件的名稱添加到數組中,然后將該數組發送到模型,我可以將其提取並添加到我的數據庫中。
但是,當我這樣做時,我的數據庫中為文件名添加的值總是為'1'。
這是我的代碼,其中我上傳並存檔。
這是控制器類的一部分,文件名更改並添加到數組中
$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;
$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);
}
這是模型
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;
}
如果你能幫助我在這里呆上幾個小時,請先謝謝你。
rename()更改系統資源的名稱,並返回Bool以獲得成功。 當你將布爾值true轉換為字符串時,你得到“1”。 如果要將名稱存儲在DB中,則應設置變量,即:
$new_name = $dir . $id . '_' . $randomstring . '.jpg';
然后調用重命名:
rename($dir . $file, $new_name);
並使用$ new_name插入數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.