2

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

2 Answers 2

1

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'
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are iterating over the wrong array, you need to do it like this:

foreach($allFiles as $file) {

    $pathToFileSingle = $file["tmp_name"];
    $destFilePath     = $file["name"];

(you also didn't set the destination filename properly)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.