Kees Sonnema
Kees Sonnema

Reputation: 5784

Upload multiple files with html5 and php to folder "uploads"

I have an upload script for uploading an image.

my directory looks like this: directory

my index.php looks likes this:

<html>

    <head>

        <!-- Link naar de stylesheet -->
        <link rel="stylesheet" type="text/css" href="style/default.css" />

    </head>
    <body>
        <center>

            <div id="title">
                <h1>Het "Vergeet-mij-nietje"</h1>
                <sup>Image Upload Script</sup>
            </div>

            <!-- Content -->
            <div id="content1">

                <?php

                    // Check if a post exist
                    if( !isset( $_POST['p'] ) ) { $_POST['p']= 0; }

                    // Include files
                    if( $_POST['p'] == 1 ){
                        include( "includes/uploadimage.php" );
                    }else{
                        include( "includes/uploadform.php" );
                    }

                ?>
            </div>
    </body>
</html>

In my includes folder i have the following files: uploadform.php uploadimage.php

My uploadform.php looks like this:

<form method="post" enctype="multipart/form-data">

    Kies hier meerdere bestanden om te uploaden
    <br /> <br />
    <input type="hidden" name="p" value="1" />

    <input type="file" name="image" multiple />

    <br /> <br />

    <input type="submit" value="Upload"/>

</form>

Notice the multiple function.

My uploadimage.php looks like this:

<?php

    // variabelen.
    $name=          $_FILES['image']['name'];
    $temp=          $_FILES['image']['tmp_name'];
    $type=          $_FILES['image']['type'];
    $size=          $_FILES['image']['size'];
    $path=          'uploads/ ' . md5( rand( 0, 1000 ) .rand( 0, 1000 ) .rand( 0, 1000 ) .rand( 0, 1000 ) ) . '.jpg';
    $size2=         getimagesize( $temp );
    $width=         $size2[0];
    $height=        $size2[1];


    // Benodigdheden
    $maxwidth=      1281;
    $maxheight=     1081;
    $allowed=       array( 'image/jpeg', 'image/png', 'image/gif' );


    // Echo data.
    echo '
        '. $name .' <br />
        '. $temp .' <br />
        '. $type .' <br />
        '. $size .' <br />
        '. $path .' <br /><br />
        '. $width .' x
        '. $height .' <br />
    ';

    if( in_array( $type, $allowed   ) ){

        if( $width < $maxwidth && $height < $maxheight ){

            if( $size < 5242880 ){

            /*
                // Vorm van de foto. Dit is nodig bij het Resizen.
                if( $width == $height ){ $case=1;}
                if( $width >  $height ){ $case=2;}
                if( $width <  $height ){ $case=3;}

                switch( $case ){

                    // Vierkant
                    case 1;

                        $newwidth=      100;
                        $newheight=     100;

                    break;

                    // Liggende Rechthoek
                    case 2;

                        $newheight=     100;
                        $ratio=         $newheight / $height;
                        $newwidth=      round( $width * $ratio );

                        echo $newwidth.'x'.$newheight;

                    break;

                    // Staande Rechthoek
                    case 3;

                        $newwidth=      100;
                        $ratio=         $newwidth / $width;
                        $newheight=     round( $height * $ratio );


                }

            */

            }else{
                echo '<p><b>De foto die u zojuist heeft geupload is te groot. Upload een foto, kleiner dan 5mb.</b></p>';
            }


        }else{
            echo '<p><b>De foto die u zojuist heeft geupload heeft een te grote resolutie. Upload een kleinere foto.</b></p>';
        }

    }else{
        echo '<p><b>De foto die u zojuist heeft geupload is geen .jpg, .png of .gif. Deze extensie is niet toegestaan.</b></p>';
    }

    // Upload files.
    move_uploaded_file( $temp, $path );

    // Terug stuur link
    echo '<a href="index.php">Ga Terug</a>';



?>

Notice that i ain't using the width and height functions


MY PROBLEM:

My problem is that the uploadimage.php can't upload multiple images. it can only upload 1 image at the time.

How do i change the code in uploadimage.php so it can upload multiple files at a time.

thanks

Upvotes: 1

Views: 1566

Answers (1)

Yves Lange
Yves Lange

Reputation: 3994

i guess you should have an array when post was send

Try to replace:

<input type="file" name="image" multiple />

With:

<input type="file" name="image[]" multiple />

In the uploadimage.php use the new the array: $_FILES['image'][i]['name'] where 'i' is the number of the image that you like to have.

Use a loop to get all of them.

Have a look: PHP Documentation http://php.net/manual/en/features.file-upload.multiple.php

Upvotes: 1

Related Questions