CodeIgniter中图片字段存储为Array,Array,Array问题求解
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
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']; } }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_imagecontains valid upload data (you can debug withvar_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

