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

SatheeshJM
  • 3,575
  • 8
  • 37
  • 60

2 Answers2

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.

lshas
  • 1,691
  • 1
  • 19
  • 39
Ronny Sherer
  • 8,349
  • 1
  • 22
  • 9
  • 2
    Can you please improve this answer by explaining what the code do? – A.L Mar 06 '15 at 12:29
  • 7
    See: http://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 '17 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 '17 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 '19 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 '19 at 23:03
  • This code is a fallback for STDOUT, STDIN, STDERR, if they are not defined. (answer to A.L) – Ronny Sherer Nov 11 '19 at 12:05
  • I ended up using `$out=fopen('php://output', 'wb');` because I wanted to fwrite to a user Browser query. Stdin is probably only relevant when PHP is called on a command line. – David Spector Jul 10 '22 at 23:47
-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'.

cdroul
  • 1
  • 2