0
votes

I am trying to take FILES in Array from HTML form, and store them in database. I have written the following code and it is giving me many error messages. It looks like that the main problem is that it is not converting Array into string.

Kindly guide me.

Line 27 : $image_name=        $_FILES["files"]["name"];  

LINE 29:  $random_name=       rand().$_FILES["files"]["name"];

$_FILES Output

Array ( [files] => Array ( [name] => Array ( [0] => Bracelet_Gold.jpg [1] => Necklaces_Silver.png [2] => Brooches_Gold.png ) [type] => Array ( [0] => image/jpeg [1] => image/png [2] => image/png ) [tmp_name] => Array ( [0] => F:\xampp\tmp\php599C.tmp [1] => F:\xampp\tmp\php599D.tmp [2] => F:\xampp\tmp\php599E.tmp ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [size] => Array ( [0] => 7150 [1] => 37867 [2] => 314296 ) ) )

<body>

<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="files[]" />
<input type="file" name="files[]" />
<input type="file" name="files[]" />
<input type="submit" value="Send" />
</p>
</form>

</body>
</html>

<?php

include 'connect.php';

if (isset($_FILES['files'])
    ||  ($_FILES["files"]["type"]   == "image/jpeg"))
    {

    foreach($_FILES['files']['tmp_name'] as $key=> $tmp_name)
        {
            //echo $tmp_name."<br>";

        echo    $image_name=        $_FILES["files"]["name"];  

            $random_name=       rand().$_FILES["files"]["name"];

            $folder="upload/products/" .$random_name;                       

            move_uploaded_file($_FILES["files"]["tmp_name"],
                        "upload/products/" . $random_name);




            $sql = "Insert into product_images (product_id,name,images) 
                            VALUES ($current_id,'$image_name', '$folder')";

                            if (mysql_query($sql))
                            {
                                echo 'Done';
                                }

                                else
                                {
                                    echo mysql_error();
                                    }


        }

    }

?> 

Notice: Array to string conversion in F:\xampp\htdocs\CMS\array_upload.php on line 27 Array Notice: Array to string conversion in F:\xampp\htdocs\CMS\array_upload.php on line 29

Warning: move_uploaded_file() expects parameter 1 to be string, array given in F:\xampp\htdocs\CMS\array_upload.php on line 34

2
Which lines are 27, 29 and 34? - John Dvorak
Somewhere, something you think is a string is actually an array. This will be on the line numbers supplied by the error messages - allen213
namely, $_FILES["files"]["tmp_name"] seems to be claimed an array. The next step is to dig up the documentation for $_FILES - John Dvorak
Thanks for the guidance, I have updated my Question. Kindly check it. - Taha Kirmani
on 27 & 29 try $_FILES['files']['name'][$key] - allen213

2 Answers

3
votes

$_FILES["files"]["tmp_name"] is the array of the files you are receiving So you need to do:-

move_uploaded_file($_FILES["files"]["tmp_name"][$key],
                        "upload/products/" . $random_name);

Or you are receiving current temp name in the $tmp_name so you can use:-

move_uploaded_file($tmp_name,
                            "upload/products/" . $random_name);
1
votes

On Lines 27 & 29 try $_FILES['files']['name'][$key]

Also replace the first parameter of move_uploaded_file with $tmp_name

move_uploaded_file($tmp_name,