26

I am trying to set up Amazon Aws Php SDK in Xampp.

After installing the SDK, I am trying to download a bucket from Amazon S3, using the following code.

<?php

error_reporting(-1);
ini_set('display_errors', 'on');

include_once ('aws/aws-autoloader.php');
use Aws\S3\S3Client;

$client = S3Client::factory(array(
     'key'    => '__my__key__',
     'secret' => '__secret__key__'
));

$destination = 'downloaded_bucket';
$source_bucket = '__my__bucket__name';
$key_prefix = '';
$options = array('debug'=>true);

$client -> downloadBucket($destination,$source_bucket,$key_prefix,$options);
?>

Now on executing this php from my browser, I get the following error.

Notice: Use of undefined constant STDOUT - assumed 'STDOUT' in __my__path\Aws\S3\Sync\AbstractSyncBuilder.php on line 294
STDOUT
Warning: fwrite() expects parameter 1 to be resource, string given in __my__path\Aws\S3\Sync\DownloadSyncBuilder.php on line 124
STDOUT
Warning: fwrite() expects parameter 1 to be resource, string given in __my__path\Aws\S3\Sync\DownloadSyncBuilder.php on line 124
STDOUT
Warning: fwrite() expects parameter 1 to be resource, string given in __my__path\Aws\S3\Sync\DownloadSyncBuilder.php on line 124

The final 3 warnings occur because of the first Notice, because instead of resource, the string 'STDOUT' is passed.

What is the reason for the first notice? The code segment for this notice is

if ($this->debug) {
            $this->addDebugListener($sync, is_bool($this->debug) ? STDOUT : $this->debug);
        }

which is part of the SDK. And the culprit for the fwrite warning code is the addDebugListener function

protected function addDebugListener(AbstractSync $sync, $resource)
    {
       //blah blah
       fwrite($resource, "Downloading {$from} -> {$to}\n");
       //blah blah
    }

My PHP version is 5.4.16

4
  • you may need to define STDOUT if its not defined, whats in line 124
    – DevZer0
    Jul 21, 2013 at 4:29
  • I think there is some other bootstrap file that is not being included that should be.. one that has define("STDOUT", "php://output");
    – Orangepill
    Jul 21, 2013 at 4:31
  • @DevZer0 A fwrite call. Have updated the question.
    – SatheeshJM
    Jul 21, 2013 at 4:40
  • 14
    STDOUT is available automatically when running PHP from the command line. Are you running this through the browser? Jul 22, 2013 at 15:28

2 Answers 2

45

The problem, in this case, is that the constant STDOUT is not defined. It is a constant available when using the command line, so in order to use them in other settings you can do this:

if(!defined('STDIN'))  define('STDIN',  fopen('php://stdin',  'rb'));
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb'));
if(!defined('STDERR')) define('STDERR', fopen('php://stderr', 'wb'));

This will check whether or not the constant has been defined, and if not, define them according to how they are expected to work.

More information about the constants can be found here in the PHP docs.

7
  • 2
    Can you please improve this answer by explaining what the code do?
    – A.L
    Mar 6, 2015 at 12:29
  • 7
    See: php.net/manual/en/features.commandline.io-streams.php PHP's CLI interface defines STDIN, STDOUT, and STDERR, which are constants that refer to file handles capable of reading from (STDIN) or writing to (STDOUT, STDERR) standard I/O streams. Because these are only defined by the CLI interface, you can't use them in a web application unless you define them yourself. The three lines above does just that, but only if they're not only defined, making it safe. In reality a web application probably doesn't need to access them frequently enough to make them constants.
    – Mark
    May 25, 2017 at 13:38
  • 1
    warning, in FPM context, STDIN is php://input , not php://stdin , and STDOUT is php://output , not php://stdout (but i think STDERR is still php://stderr )
    – hanshenrik
    Dec 16, 2017 at 10:53
  • @hanshenrik, No, unfortunately, I've just got here because STDERR is undefined in my FPM setup. Can anybody please tell why? ("web application probably doesn't need to access them frequently" that's true, but I'm not quite sure disabling by default every infrequently used feature is a good idea. :) )
    – Sz.
    Aug 23, 2019 at 18:18
  • @Sz. if you're looking for the raw http request body, then it's in php://input, what do you want from STDIN anyway?
    – hanshenrik
    Aug 23, 2019 at 23:03
-5

You should add, in your own code, something like :

define("STDOUT", fopen('log.txt', 'w'));

Information about files transfered will be logged into the file 'log.txt'.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.