0

I am attempting to upload a file to s3 using php 5.3.3. I am using the Amazon PHP sdk and using the autoloader. The problem is that the autoloader for AWS is not loading the classes correctly and we are getting a exception when loading S3. Our server structure looks like this:

->public (www document root)

->lib

->->aws

So our upload code is located in /public/ and our AWS library is located at /lib/aws/ so to get from public to lib we do /../lib/aws/.

Here is what the upload.php code looks like in the public folder which initiates the amazon upload:

require $_SERVER['DOCUMENT_ROOT'].'/../lib/aws/aws-autoloader.php';

use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;

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

It successfully works and prints the echo 'creating...' output and then we get an error.

Here is what the exception looks like:

2014-04-12 19:46:40 UTC     creating...  
2014-04-12 19:46:40 UTC     ( Exception Object
2014-04-12 19:46:40 UTC     [message:protected] => The class S3Client could not be loaded
2014-04-12 19:46:40 UTC     [string:Exception:private] =>
2014-04-12 19:46:40 UTC     [code:protected] => 0
2014-04-12 19:46:40 UTC     [file:protected] => /public/upload.php
2014-04-12 19:46:40 UTC     [line:protected] => 26
2014-04-12 19:46:40 UTC     [trace:Exception:private] => Array
2014-04-12 19:46:40 UTC         (
2014-04-12 19:46:40 UTC             [0] => Array
2014-04-12 19:46:40 UTC                 (
2014-04-12 19:46:40 UTC                     [function] => __autoload
2014-04-12 19:46:40 UTC                     [args] => Array
2014-04-12 19:46:40 UTC                         (
2014-04-12 19:46:40 UTC                             [0] => S3Client
2014-04-12 19:46:40 UTC                         )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC                 )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC             [1] => Array
2014-04-12 19:46:40 UTC                 (
2014-04-12 19:46:40 UTC                     [file] => /public/upload.php
2014-04-12 19:46:40 UTC                     [line] => 15
2014-04-12 19:46:40 UTC                     [function] => spl_autoload_call
2014-04-12 19:46:40 UTC                     [args] => Array
2014-04-12 19:46:40 UTC                         (
2014-04-12 19:46:40 UTC                             [0] => S3Client
2014-04-12 19:46:40 UTC                         )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC                 )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC             [2] => Array
2014-04-12 19:46:40 UTC                 (
2014-04-12 19:46:40 UTC                     [file] => /public/upload.php
2014-04-12 19:46:40 UTC                     [line] => 408
2014-04-12 19:46:40 UTC                     [args] => Array
2014-04-12 19:46:40 UTC                         (
2014-04-12 19:46:40 UTC                             [0] => /public/upload.php
2014-04-12 19:46:40 UTC                         )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC                     [function] => include
2014-04-12 19:46:40 UTC                 )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC         )
2014-04-12 19:46:40 UTC
2014-04-12 19:46:40 UTC     [previous:Exception:private] =>
2014-04-12 19:46:40 UTC )

We are using the latest version of AWS from: https://github.com/aws/aws-sdk-php

We are using PHP 5.3.3 and also the flourishlib: http://flourishlib.com/

That also has a flourishlib autoload function which looks like this:

function __autoload($class_name)
{
    // Customize this to your root Flourish directory
    $flourish_root = $_SERVER['DOCUMENT_ROOT'].'/../lib/flourishlib/';

    $file = $flourish_root . $class_name . '.php';

    if (file_exists($file)) {
        include $file;
        return;
    }

    throw new Exception('The class ' . $class_name . ' could not be loaded');
}
spl_autoload_register('__autoload');

I think what is happening is the flurishlib autoload function is trying to load the amazon classes and its causing a error.

How can I make amazon use the correct autoload function?

pawelglow
  • 708
  • 1
  • 10
  • 20

2 Answers2

0

Strike that, reverse. Nobody should ever still be using the __autoload() function. This was deprecated many years ago in favor of the stackable spl_autoload_register() function.

From the code snippet you posted, FlourishLib is doing the wrong thing by having a function called __autoload() at all.

Ryan Parman
  • 6,855
  • 1
  • 29
  • 43
  • Well the __autoload function is not being registered automatically that is why at the bottom we have the spl_autoload_register function. Calling it a __autoload makes no difference I think. – pawelglow Apr 14 '14 at 13:53
0

I think the only problem here is that you are not reference the S3Client class properly. In no place above did I see you use the fully-qualified class name. Try adding

use Aws\S3\S3Client;

It also looks like you don't need the other two use statements (at least in the code you provided), because you are not actually using either of those two classes. You should be able to remove

use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;

And yes, I agree with Ryan that __autoload is bad, but it appears you are handling it correctly by registering with the spl autoload chain.

Jeremy Lindblom
  • 6,437
  • 27
  • 30