Search code examples
phpamazon-s3file-upload

Multiple file upload in to amazon not working using php code


I want to upload multiple files in to amazon using S3 API.

This is my code

<form method="POST" enctype="multipart/form-data" action="/uploadcontentintoamazone">
<input type="file" id="fileinputAllPages" name="files[]" multiple />
</form>

uploadcontentintoamazone.php

$allFiles        = $_FILES['files'];

$s3 = S3Client::factory(array(
    'key'    => '*************************',
    'secret' => '*************************'
));

foreach($allFiles['tmp_name'] as $file){

    $pathToFileSingle = $file;
    $destFilePath     = 'destination file name';

    $commands[]           = $s3->getCommand('PutObject', array(
        'Bucket'          => 'application-data',
        'Key'             => $destFilePath.$file,
        'SourceFile'      => $pathToFileSingle,
        'ACL' => 'public-read'
    ));

}

$s3->execute($commands);
foreach ($commands as $command) {
    $result = $command->getResult();
}

API integration is working perfectly. But temp file is uploaded instead of my uploaded file.

Thanks in advance


Solution

  • try this way:

    for($i=0; $i<count($_FILES['files']['name']); $i++) {    
        $pathToFileSingle = $_FILES['upload']['tmp_name'][$i];
        $destFilePath     = 'destination file name' . $_FILES['upload']['name'][$i];
    
         $commands[]           = $s3->getCommand('PutObject', array(
            'Bucket'          => 'application-data',
            'Key'             => $destFilePath,
            'SourceFile'      => $pathToFileSingle,
            'ACL' => 'public-read'
        ));
    }