Kim Carlo
Kim Carlo

Reputation: 1223

Uploading multiple files error;

I have this code below(I'm working on file uploads). at localhost it's working properly but when i upload it to my server it returns this error;

Parse error: syntax error, unexpected '[' in /home/public_html/bookingsuccess.php on line 91

line 91 which is the first line of code below:

$allowed = ['jpg','png','gif','eps','pdf','doc','docx','xls','xlsx','ppt','pptx','ai','zip','rar'];
$succeeded = [];
$failed = [];
if (!empty($_FILES['file'])) {
    include('config.php');

    foreach ($_FILES['file']['name'] as $key => $name) {

        if($_FILES['file']['error'][$key] === 0){
            $temp = $_FILES['file']['tmp_name'][$key];
            $ext = explode('.', $name);
            $ext = strtolower(end($ext));

            $file = md5_file($temp) . time() .'.'.$ext;

            if (in_array($ext,$allowed) === true && move_uploaded_file($temp, "uploads/{$file}") === true) {
                    print_r($succeeded [] = array('name' => $name, 'file' => $file));
                    $dir = "uploads/{$file}";
                    $qry = $handler->prepare('INSERT INTO store (location, name) VALUES (?,?)'); 
                    $qry->execute(array($dir, $name));
                # code...
            }else{
                $failed[] = array($name);
                echo "Some files failed to upload due to invalid file extensions";
            }

        }else{

            echo "Error";
        }
    }
}

Thanks for any response!

Upvotes: 0

Views: 50

Answers (1)

WillS
WillS

Reputation: 362

You probably are using PHP < 5.4.

According to http://docs.php.net/manual/en/language.types.array.php:

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

This is shown in the example on the manual page with:

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

Upvotes: 1

Related Questions