Multiple file upload problem

Hey,

I have the following code which inserts filenames from an array of input images into a database AND also upload the images onto the server…


    public function saveImageNamesFromIdeasPage(){

        $query = "SELECT MAX(ID) as max FROM ideas";
        $result = mysql_query($query);
        $max = mysql_fetch_array($result);
        $ideasID = $max['max'];

        $root = $_SERVER['DOCUMENT_ROOT']. "/kidsintranet/public_html/ideasfiles/";
                    
        foreach($_FILES['image']['name'] as $image){

            if($image != ''):
                $target_path = $root . basename($image);
                move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
            endif;

            $sql = "INSERT INTO ideas_files
                    (ideasID, ideas_files, deleted)
                    VALUES (
                        ".$ideasID.",
                        '".$image."',
                        0
                    )";
            $result = mysql_query($sql);
        }
    }

The INSERT works perfectly, so if for example there are 10 images, ALL the filenames are successfully inserted. A typical input file is as so:

<input type=“file” name=“image” id=“image1”/>
<input type=“file” name=“image” id=“image2”/>
<input type=“file” name=“image” id=“image3”/>
<input type=“file” name=“image” id=“image4”/>



<input type=“file” name=“image” id=“image10”/>

And so on…

But the images are not being uploaded? I get this error:

Notice: Array to string conversion in /public_html/kidsintranet/application/model/db/Ideas.class.php on line 112

Which is referring to this line:

move_uploaded_file($_FILES[‘image’][‘tmp_name’], $target_path);

Any ideas what the problem is?

Thanks

Thanks Anthony! I had to make some slight changes as the filepath was causing as issue…

This is my final method:


    public function saveImageNamesFromIdeasPage(){

        $query = "SELECT MAX(ID) as max FROM ideas";
        $result = mysql_query($query);
        $max = mysql_fetch_array($result);
        $ideasID = $max['max'];

        $root = $_SERVER['DOCUMENT_ROOT']. "/kidsintranet/public_html/ideasfiles/";

        foreach($_FILES['image']['error'] as $key => $error){
            $target_path = $root.basename($_FILES['image']['name'][$key]);

            if(UPLOAD_ERR_OK === $error){
                move_uploaded_file($_FILES['image']['tmp_name'][$key], $target_path);
                mysql_query(
                    sprintf(
                        "INSERT INTO ideas_files (ideasID, ideas_files, deleted)VALUES(%d, '%s', false)",$ideasID,
                        mysql_real_escape_string($_FILES['image']['name'][$key])
                    )
                );
            }
        }
    }

:smiley: Works now…

You’re accessing the $_FILES data incorrectly, see: http://www.php.net/manual/en/features.file-upload.post-method.php


<?php
foreach($_FILES['image']['error'] as $key => $error){
    
    if(UPLOAD_ERR_OK === $error){
        
        move_uploaded_file(
            $_FILES['image']['tmp_name'][$key],
            $_SERVER['DOCUMENT_ROOT']. '/kidsintranet/public_html/ideasfiles/'
        );
        
        mysql_query(
            sprintf(
                "INSERT INTO ideas_files (ideasID, ideas_files, deleted)VALUES(&#37;d, '%s', false)",
                $max,
                mysql_real_escape_string($_FILES['image']['name'][$key])
            )
        );
        
    }
}
?>

Hmm i did a var_dump($image)…

I got this:

string(16) “image.png”

What does this mean? :confused:

Well, the function you’re using requires a String, not an Array. Hence the notice. :wink:

A simple var_dump should do the trick.

Pretty sure it is an array, how would i be able to check for this?

Is there an obvious mistake somewhere? :cool:

I guess the $image variable is an Array and not a String. Have you checked for this?