doufutao4428 2011-04-13 01:57
浏览 56
已采纳

数组中的变量值作为整数返回,但在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;
        $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.

  • 写回答

1条回答 默认 最新

  • doupafu6980 2011-04-13 02:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算