-1

I am lowering the file name and checking for the file extension using explode() but I keep getting these two errors: Warning: explode() expects parameter 2 to be string, array given On this line: $tmp = explode('.', $_FILES['files']['name']); and Warning: end() expects parameter 1 to be array, null given on this line $file_ext = strtolower(end ($tmp)); (the second on comes from the first one).

I think this is the problem $file_name = count($_FILES['files']['name']); specifically the count part. because this is a file uploading script and I'm am giving someone the option to upload multiple images at a time. When I click the upload button the images still go in but I just get those two warnings. I tried all the other answers on here but none of them helped me so any suggestions ?

$date_time = date('Y-m-d_H-i-s');
$errors = [];

if(isset($_POST['submit'])) {

    //if (isset($_POST['files'])) {

        $file_name = count($_FILES['files']['name']);
        $file_size = $_FILES['files']['size'];                       
        $file_type = $_FILES['files']['type'];
        $tmp = explode('.', $_FILES['files']['name']);
        $file_ext = strtolower(end ($tmp));

        $extensions = array("jpeg", "jpg", "png", "gif");

        if(in_array($file_ext, $extensions) === false) {

            $errors = "Only jpeg, jpg, png and gif files are allowed.";
        }

        if ($file_size > 95097152) {

            $errors = 'File cannot be larger than 1MB';
        }
    

        //$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.

        // Count # of uploaded files in array
        //$total = count($_FILES['files']['name']);

        // Loop through each file
        for( $i=0 ; $i < $file_name ; $i++ ) {

            //Get the temp file path
            //$tmpFilePath = $_FILES['files']['tmp_name'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];

            //Make sure we have a file path
            if ($file_tmp != ""){

                    # code...

                
                //Setup our new file path
                $newFilePath = "./uploads/" . $_FILES['files']['name'][$i];

                //Upload the file into the temp dir
                if(move_uploaded_file($file_tmp, $newFilePath)) {

                    //Handle other code here

                }
            }
        }
    //}
}
John H
  • 1
  • 4
  • First of all do print_r($_FILES['files']['name']) and check what is the value and then write the code according to it. $_FILES['files']['name'] must have an array as value. – Brijesh Borad Apr 19 '21 at 20:27

2 Answers2

0

Issue from the second parameter of explode function. Your $_FILES['files']['name'] is not a string, but must be. For checking that:

  1. var_dump($_FILES['files']['name']); die('test');
  2. You will see the array
  3. You must understand which parameter you need and use it as second parameter of explode

P.S. for fast checking you can instead $_FILES['files']['name'] manually write some string, for example test.jpg

I hope my answer will help you.

0

Try moving the everything except $file_name = count($_FILES['files']['name']); into your for loop. Reason being is that $_FILES['files']['name'] is an array containing all of your uploaded files, explode() is in this case expecting a single file name. You'll get that from your looping through individual files.

Here's an example of a multiple file upload using a similar method: Multiple file upload in php

DLzer
  • 159
  • 11