When attempting to upload multiple files in CodeIgniter, the function is_uploaded_file() is expecting a string as parameter 1, but an array

I am having trouble uploading multiple files.

Below is my form code:

<form method="POST" action="<?=base_url()?>register/saverecord" enctype="multipart/form-data">
        <input type="file" name="file_upload[]"  multiple="multiple" value=""><br/><br/>
        <input type="submit" name="submit" value="SUBMIT">
    </form>

This is the function I use for multiple file upload:

public function saveRecord() {
        $config['upload_path'] = APPPATH . './uploads/';
        $path = $config['upload_path'];
        $config['allowed_types'] = '*';
        $config['max_size'] = '1024';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';
        $this->load->library('upload', $config);
        $fileName = [];
        foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
        {
            if (!empty($fileObject['name'])) {
                $this->upload->initialize($config);
                if (!$this->upload->do_upload($fieldname)) {
                    $errors = $this->upload->display_errors();

                } else {
                    $fileName[] = $this->upload->data();
                }
            }
        }
        echo "<pre>";
        print_r($fileName);
        echo "</pre>";
        exit;
    }

Here is the error message I encountered during upload: https://i.stack.imgur.com/c1s1c.png

I found a solution on this URL:Upload multiple files in CodeIgniter

Answer №1

if (!$form->upload->submit($fieldName)) {

Please make sure to include individual files instead of an array for the $fieldName parameter in this section.

Answer №2

Give this code a try, it should work fine. The issue lies in passing an array to the do_upload function, which is not valid. I have made the necessary corrections in the code, so please check it out after replacing the existing code.

public function saveRecord() {
        $config['upload_path'] = APPPATH . './uploads/';
        $path = $config['upload_path'];
        $config['allowed_types'] = '*';
        $config['max_size'] = '1024';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';
        $this->load->library('upload', $config);
        $fileName = [];
        foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
        {
            if (!empty($fileObject['name'])) {
                $this->upload->initialize($config);
                if (!$this->upload->do_upload($fileObject['name'])) {
                    $errors = $this->upload->display_errors();

                } else {
                    $fileName[] = $this->upload->data();
                }
            }
        }
        echo "<pre>";
        print_r($fileName);
        echo "</pre>";
        exit;
    }

Answer №3

When dealing with a multiple file upload, you can extract information from the files using this method - how to integrate this with the built-in functions provided by codeigniter remains unclear

foreach( $_FILES[ 'fieldname' ] as $i => $void ){
    $name=$_FILES[ 'fieldname' ]['name'][$i];
    $tmp=$_FILES[ 'fieldname' ]['tmp_name'][$i];
    $size=$_FILES[ 'fieldname' ]['size'][$i];
    $type=$_FILES[ 'fieldname' ]['type'][$i];

    /* other code */

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve foreign elements in a Laravel many-to-many relationship that are not currently associated with the main element

When dealing with Laravel's Many to Many relationship, such as the one outlined in the Laravel Documentation example available at this link, there are specific database tables set up: users id - integer name - string roles id - integer ...

Laravel typically adds the "php" string as a prefix to the result

Every time I attempt to send back a view from views in Laravel, it automatically includes "Php" as a prefix. The same issue arises when I try to return JSON using Ajax. For example, if I am returning an array ("data"=> "success") in a .php file, but up ...

Track the number of views each month using PHP and MySQL to generate dynamic jQuery charts

I am currently utilizing jQuery with chart.js to display the view counter based on month using PHP and MySql. Each page view is inserted into MySql in the following format : | id | ip | page | date(timestamp) | | 1 | 138.86.20.20 | test.p ...

Is there a way to choose an entire directory containing images using codeigniter?

For my current project, I am looking to choose an entire folder that contains pictures and apply a transformation to each picture within the folder. This process is akin to using scripts in Photoshop for making bulk adjustments to images. ...

Symfony .htaccess - troubleshooting 500 error on third level domain

My application is located in a subdirectory at www.example.com/application/v1 and I have a third-level domain set up at v1.example.com. I am using Symfony2 and this is the content of my .htaccess: DirectoryIndex app.php <IfModule mod_negotiation.c> ...

Deciphering GB2312 encoding with PHP

Currently, I am developing an IMAP email script and have encountered some lines of code in GB2312 (which seems to be Chinese encoding). The code format looks like this: =?GB2312?B?foobarbazetc I am curious about how I can begin working with this string. A ...

Decipher the JSON code to modify the data

I have a JSON array below. {"entries":[{"uid":155551338258538,"photo":"https:\/\/m.ak.fbcdn.net\/profile.ak\/hprofile-ak-prn1\/323887_155551338258538_1152153357_q.jpg","type":"user","text":"shikhadamodar","path":"\/shikha.dam ...

Combining JSON arrays in PHP derived from MySQL query outcomes

The current code is as follows: function getList(){ $sql = 'SELECT DISTINCT `person`.`person_name` as name, `skill`.`skill_name` as skill,`skill_linker`.`skill_score`; $result = $GLOBALS['conn']->query($sql); if ($res ...

Ways to analyze three PHP arrays and organize outcomes that are not duplicated into separate new arrays

I have an array containing three nested arrays. $rubros array(3) { [1]=> array(8) { [0]=> array(1) { ["idRubro"]=> string(1) "1" } [1]=> array(1) { ["idRubro"]=> str ...

Is there a way to incorporate the information from PHP files into the output produced by JavaScript?

I am currently working on a JavaScript script that scrapes data and displays the result on the screen successfully. However, I now face a challenge in wrapping this output with pre and post content from PHP files for formatting purposes. Here is an overvi ...

Guide to building an interactive slider with PHP, MySQL, and jQuery

I had a vision to develop a website with dynamic content, specifically a slider similar to this. Currently, it is hard coded and I am looking to make it dynamic by loading images from a folder in my-sql db. I want to use a php script to update the databa ...

Different methods to send dynamically created vuejs array data to a mysql database

I'm currently utilizing this code in my LARAVEL project http://jsfiddle.net/teepluss/12wqxxL3/ The cart_items array is dynamically generated with items. I am seeking guidance on looping over the generated items and either posting them to the databa ...

PHP - converting an array to JSON format and returning it

Let me start by mentioning that I have explored various Stack Overflow threads on this topic, yet I still find myself perplexed. Within my PHP code, I retrieve data from my database and attempt to store it in an array like so: $arrayResult = array(); ...

What is the method for appending an element to an array in PHP?

I've been trying to work with this code snippet, but it's not functioning as expected. Regardless of how many items I add, it only shows one item in the array. Could someone please point out where I might be making a mistake? session_start(); ...

I am experiencing excessive paper skipping in my printer

I have been using the 80 column dot matrix printer. However, after each printout, the paper skips two times resulting in a lot of wasted paper. How can I resolve this issue? Currently, I am only utilizing the window.print() JavaScript function. Are there ...

Error: A SyntaxError was encountered due to a missing closing parenthesis after an argument list while writing JavaScript within PHP code

I'm facing an issue writing JavaScript within PHP code. Here's my script : echo ' <script>'; echo ' $(function(){'; echo ' x = parseInt($("#counter2").val());'; echo ' $("#add_row2").click(function(){&apo ...

A guide on uploading a photo to an existing mySQL database using a form

I already have a mySQL database containing basic client profiles, but now I want to add a picture for each record. I've set up a form with inputs and textareas to create new records. However, the field for the photo is stored as a blob. How can I inc ...

Implementing popup alert for multiple tabs when Javascript session times out

I have implemented javascript setInterval() to monitor user idle time and display a popup alert prior to automatic logout. However, it seems to be functioning correctly only in single tabs. Here is the code snippet: localStorage.removeItem("idleTimeValue ...

What mistake did I make when trying to use the Haversine formula in PHP?

I have 3 functions which seem similar but produce different results... class GeoCalculations { const EARTH_RADIUS = 6371000; public function degToRad($deg) { return $deg * pi() / 180; } public function haversineDistance($lat ...

Creating image galleries with PHP and MYSQL - Maximizing the potential of your image data

As I work on developing my blog, I've come across a question regarding how to handle featured images for each post. Specifically, I'm torn between two options for managing image data... One option is to store the image data directly in my MYSQL ...