homerun
homerun

Reputation: 20745

PHP upload input array and how to find how many files?

I have this form :

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="file" name="photo[]" />
    <input type="file" name="photo[]" />
</form>

And I upload the files with php this way :

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        $FV_filename = $_FILES['photo']['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file($_FILES['photo']['tmp_name'][$i], uniqid().$fileTYPE);
    }
}

upFiles($_POST['howManyFiles']);

As you can see I get from the the client side how many files have been sent. What I want to do is to check in the server side how many files have been received. How can I do that? Is there any built in php function, if not then how to create one?

Upvotes: 4

Views: 6877

Answers (3)

Rijk
Rijk

Reputation: 11301

count( $_FILES['photo']['name'] );

should work. Alternatively, just rewrite your for loop to a foreach:

foreach ( $_FILES as $name => $file ) {
    var_dump( $file );
    // process file...
}

Upvotes: 4

SteAp
SteAp

Reputation: 11999

Iterate over the $_FILES array:

function upFiles( $baseVarName ){

    foreach( $_FILES[ $baseVarName ] as $fileDetails ) {

        $FV_filename = $fileDetails['name'][$i];
        $fileTYPE = substr($FV_filename , -4);
        move_uploaded_file( $fileDetails['tmp_name'][$i], uniqid().$fileTYPE);

    }

}

upFiles( 'photo' );

Upvotes: 0

Bouke
Bouke

Reputation: 12138

You could check whether the file has been uploaded with is_uploaded_file, like so:

function upFiles($files){
    for($i = 0 ; $i < $files ; $i++){
        if (is_uploaded_file($_FILES['photo']['tmp_name'][$i])) {
            // uploaded file
        }
    }
}

You could use foreach to loop over all the uploaded photos instead of using a magic number.

From: php.net

Upvotes: 0

Related Questions