You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中图片字段存储为Array,Array,Array问题求解

Fix for "Array,Array,Array" in Image Field (PHP/CodeIgniter)

Hey there! I totally get why this is confusing—let's break down why your image field is storing Array,Array,Array and fix it.

The Root Problem

When you use implode(',', $post_image), you're assuming $post_image is an array of strings (like filenames). But in reality, each element in $post_image is itself an array—this is how CodeIgniter returns uploaded file data (each entry has keys like file_name, tmp_name, size, etc.). When you try to implode an array of arrays, PHP converts each inner array to the string "Array", hence the Array,Array,Array result.

Step-by-Step Fix

  1. Extract the actual data you need from each image array
    You probably want to store filenames (or file paths) instead of the full upload arrays. Use one of these methods:

    Option 1: Use array_column() (PHP 5.5+)

    This is the cleanest way to grab a single value from each inner array:

    // Extract just the "file_name" values from each image entry
    $image_filenames = array_column($post_image, 'file_name');
    

    Option 2: Loop through the array (for older PHP versions)

    If you're stuck on an older PHP version, loop through each entry to collect filenames:

    $image_filenames = [];
    foreach ($post_image as $img) {
        // Make sure we're dealing with a valid array and the file_name exists
        if (is_array($img) && isset($img['file_name'])) {
            $image_filenames[] = $img['file_name'];
        }
    }
    
  2. Implode the extracted filenames
    Now that you have an array of strings, implode will work as expected:

    $image = implode(',', $image_filenames);
    

Full Modified Function

Here's how your updated create_post function should look:

public function create_post($post_image){
    $slug = url_title($this->input->post('title'));
    
    // Extract filenames from the upload array
    $image_filenames = array_column($post_image, 'file_name');
    // Uncomment below if using PHP <5.5
    /*
    $image_filenames = [];
    foreach ($post_image as $img) {
        if (is_array($img) && isset($img['file_name'])) {
            $image_filenames[] = $img['file_name'];
        }
    }
    */
    
    $image = implode(',', $image_filenames);
    
    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'body' => $this->input->post('body'),
        'category_id' => $this->input->post('category_id'), // Fill in your actual category field
        'image' => $image
    );
    
    return $this->db->insert('posts', $data); // Replace 'posts' with your table name
}

Quick Notes

  • Double-check that $post_image contains valid upload data (you can debug with var_dump($post_image); to see the structure).
  • If you need to store full file paths instead of just filenames, replace 'file_name' with 'full_path' (or whatever key matches your upload setup).
  • Make sure your upload process is successfully saving files to your server before storing the filenames in the database!

内容的提问来源于stack exchange,提问作者Harun Anwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:09:24