php codeIgniter: syntax error, unexpected '['

0

The php codeIgniter application, which runs on windows, when ported to the server gives the following error:

  

Parse error: syntax error, unexpected '[' in /home/givix/domains/givix.com.br/public_html/givix/application/libraries/Amazon3integration_lib.php on line 79

     

A PHP Error was encountered

     

Severity: Parsing Error

     

Message: syntax error, unexpected '['

     

Filename: libraries / Amazon3integration_lib.php

     

Line Number: 79

     

Backtrace:   (...)

Being locally not. So I do not think it's logical, I thought it was some server configuration.

I have made some modifications to the .htaccess file, but I do not know anyone has ever had a similar problem.

Remember that the local machine is windows and the server is linux. (cent)

Local configuration:

  • win 10
  • easyphp 16
  • Apache 2.4.18 x86
  • PHP 5.6.19 x86
  • MySQL 5.7.11 x86

Web Configuration:

  • I'll check

The page that the error is set is this:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');


class amazon3integration_lib
{

    public $bucket_name = "teste";
    public $region = 'eu-west-1';
    public $version = 'latest';
    public $scheme = 'http';

    public $s3Client = null;

    public function __construct(array $config = array(), $reset = TRUE)
    {
         $reflection = new ReflectionClass($this);
        if ($reset === TRUE)
        {
                $defaults = $reflection->getDefaultProperties();
                foreach (array_keys($defaults) as $key)
                {
                        if ($key[0] === '_')
                        {
                                continue;
                        }

                        if (isset($config[$key]))
                        {
                                if ($reflection->hasMethod('set_'.$key))
                                {
                                        $this->{'set_'.$key}($config[$key]);
                                }
                                else
                                {
                                        $this->$key = $config[$key];
                                }
                        }
                        else
                        {
                                $this->$key = $defaults[$key];
                        }
                }
        }
        else
        {
                foreach ($config as $key => &$value)
                {
                        if ($key[0] !== '_' && $reflection->hasProperty($key))
                        {
                                if ($reflection->hasMethod('set_'.$key))
                                {
                                        $this->{'set_'.$key}($value);
                                }
                                else
                                {
                                        $this->$key = $value;
                                }
                        }
                }
        }

        //AWS account setting
        define('AWS_ACCESS_KEY',"AWS-KEY");
        define('AWS_SECRET_KEY',"AWS-SECRET");

        define('BUCKET_NAME',$this->bucket_name);//The bucket name you want to use for your project
       // define('AWS_URL','http://s3-eu-west-1.amazonaws.com/'.$this->bucket_name.'/');
        define('AWS_URL','http://'.$this->bucket_name.'.s3.amazonaws.com/');

        //check AWS access key is set or not
        if(trim(AWS_ACCESS_KEY,"{}")=="AWS_ACCESS_KEY")
        {
            exit("CI S3 Integration configuration error! Please input the AWS Access Key, "
                        . "AWS Secret Key and Bucket Name in applicatin/libraries/cis3integration_lib.php file");
        }
        require_once('amazonSdk/aws-autoloader.php');   

        //Create S3 client
        $sharedConfig = [
            'region'  => $this->region,
            'version' => $this->version,
            'scheme' => $this->scheme,
            'credentials' => [
                'key'    => AWS_ACCESS_KEY,
                'secret' => AWS_SECRET_KEY,
            ],
        ];
        $sdk = new Aws\Sdk($sharedConfig);
        $this->s3Client = $sdk->createS3();                
    }

    /**
     * Delete S3 Object
     *
     * @access public
     */     
    function delete_s3_object($file_path)
    {
            $response = $this->s3Client->deleteObject(array(
                'Bucket'     => $this->bucket_name,
                'Key'        => $file_path
            ));
            return true;
    }

    /**
     * Copy S3 Object
     *
     * @access public
     */ 
    function copy_s3_file($source,$destination)
    {
            $response = $this->s3Client->copyObject(array(
                'Bucket'     => $this->bucket_name,
                'Key'        => $destination,
                'CopySource' => "{$this->bucket_name}/{$source}",
            ));
            if($response['ObjectURL'])
            {
                return true;
            }
            return false;
    }

    /**
     * Create a new bucket in already specified region
     *
     * @access public
     */ 
    function create_bucket($bucket_name="",$region="")
    {
            $promise = $this->s3Client->createBucketAsync(['Bucket' => $bucket_name]);
            try {
                $result = $promise->wait();
                return true;
            } catch (Exception $e) {
                //echo "exception";exit;
                //echo $e->getMessage();
               return false;
            }       
    }
}

The snippet that it throws the error is this:

 $sharedConfig = [
        'region'  => $this->region,
        'version' => $this->version,
        'scheme' => $this->scheme,
        'credentials' => [
            'key'    => AWS_ACCESS_KEY,
            'secret' => AWS_SECRET_KEY,
        ],
    ];
    
asked by anonymous 01.09.2016 / 03:12

1 answer

6

Replace the code that says it is wrong with this:

$sharedConfig = array(
    'region'  => $this->region,
    'version' => $this->version,
    'scheme' => $this->scheme,
    'credentials' => array(
        'key'    => AWS_ACCESS_KEY,
        'secret' => AWS_SECRET_KEY,
    ),
);

Possibly the server should be in a PHP version prior to 5.4 and therefore it does not accept the syntax above.

See explanation on the link: link

Link Highlight Says: From PHP 5.4 you can also use the contracted array syntax, which exchanges array () for [].

I recommend asking the server (or host) to upgrade PHP, otherwise you may come across other similar errors when trying to use third-party libraries with more current code. (as is the case by what I see)

Good luck!

    
01.09.2016 / 04:17