Debug School

rakesh kumar
rakesh kumar

Posted on

How to send multiple file using file_get_contents through array concept

file_get_contents(): Argument #1 ($filename) must be of type string, array given.

The error message "file_get_contents(): Argument #1 ($filename) must be of type string, array given" suggests that you are passing an array to the file_get_contents() function instead of a string.

The file_get_contents() function is used to read the contents of a file into a string. It takes the filename as its first argument and returns the contents of the file as a string. If you pass an array as the first argument, you will get the error message you are seeing.

To fix this error, make sure that you are passing a string that contains the filename to the file_get_contents() function. If you are using an array to specify the filename, you can use the implode() function to convert the array to a string before passing it to file_get_contents(). Here's an example:

$filename = ['example.txt'];
$content = file_get_contents(implode('/', $filename));
Enter fullscreen mode Exit fullscreen mode

In this example, the implode() function is used to join the elements of the $filename array with a slash separator, which results in the string "example.txt". This string is then passed to the file_get_contents() function to read the contents of the file into the $content variable.

in client side microservices

if (isset($input['pdf_files'])) {
                    foreach ($input['pdf_files'] as $pdf_file) {
                        $content = file_get_contents($pdf_file);
                        $multipartArray[] = [
                            'name' => 'pdf_files[]',
                            'contents' => $content,
                            'filename' => basename($pdf_file),
                        ];
                    }
                }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)