[php] Fatal error: Namespace declaration statement has to be the very first statement in the script in

I'm trying to use this to create a image form upload for my website, the reason I'm using this is because it's more secure than doing everything myself (but if someone could point another working script I would be appreciated)

simon-eQ / ImageUploader

Fatal error: Namespace declaration statement has to be the very first statement in the script in C:\xampp\htdocs\project\lib\ImageUploader.php on line 4

Looking at the source code:

<br />
<b>Fatal error</b>:  Namespace declaration statement has to be the very first statement in the script in <b>C:\xampp\htdocs\project\lib\ImageUploader.php</b> on line <b>4</b><br />

I've tried with includes, requires and it still doesn't work.

This question is related to php image-upload

The answer is


I just discovered an easy way to raise this exception; let your text editor default to saving as UTF-8. Unless you can force it to omit the Byte Order Mark, which may not be easy or even possible, depending on your editor, you'll have a "character" in front of the opening <, which will be hidden from view.

Fortunately, when that possibility crossed my mind, I switched my editor (UltraEdit) into hexadecimal mode, which hides nothing, not even Byte Order Marks. Having written code that processes Byte Order Marks for many years, I recognized it instantly.

Save file as ANSI/ASCII, and accept the default 1252 ANSI Latin-1 code page, problem solved!


I had the same error message after running out of drive space. When I realized somehow the php file was corrupted, I copied the content and created a new file (I´m using Sublime Text for editing), and it solved my problem


UTF-8 Byte Order Mark at the beginning of the document
^THIS for me solved the issue. I opened the inolved file with an HEX editor and removed the BOM at the start of the file.


I have also faced the problem. In the php file, I have written following code where there was some space before php start tag

<?php
namespace App\Controller;

when I remove that space, it solved.


There is a mistake in its source. Check out its source if you may. It reads like this

<?php
    class BulletProofException extends Exception{}

    namespace BulletProof;
    ....

That is insane. Personally, I'd say the code is well documented, and elegant, but the author missed a simple point; he declared the namespace within the class. Namespace whenever used should be the first statement.

Too bad I am not in Github; could have pulled a request otherwise :(


If your using an IDE, you must start your code at the very first line. example Im using aptana studio3

//line1<?php
//line2  your code
//line3  your code
....
....

Hope it helps.That solves my problem,.


The library isn't working at all, setSizeLimit is broken, setImageSize is ignored as-well, just don't use this.

$result = $newUpload
                ->setFileTypes(array("jpg", "gif", "png", "jpeg"))
                ->setSizeLimit(array("min"=>100, "max"=>100000))
                ->setImageSize(array("height"=>200, "width"=>200))
                ->uploadTo('ads/')
                ->save($_FILES['newad_image']);
                $page.=$result;

Gives this:

Notice: Undefined offset: 1 in C:\xampp\htdocs\project\lib\ImageUploader.php on line 229

Notice: Undefined offset: 0 in C:\xampp\htdocs\project\lib\ImageUploader.php on line 229

EDIT: The size seems to be in Bytes, even though the error says Kilobytes.


If you look this file Namespace is not the first statement.

<?php
class BulletProofException extends Exception{}

namespace BulletProof;

You can try to move the namespace over the class definition.


This thread seems to be talking about the same issue - it sounds like this error is usually caused by having some data sent out of the server before the namespace statement is encountered.

  1. Could your web hosting be inserting some code into your page before the PHP code?
  2. Is there a UTF-8 Byte Order Mark at the beginning of the document?

On the other hand, it could also be a bug in ImageUploader... the main PHP file puts the namespace after the class definition, which I haven't seen in the PHP documentation, which says it should be the very first PHP code. From this page:

Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.

There's no declare keyword here, so perhaps this is a bug in the source code that slips by the developer's version of PHP, because he doesn't put the namespace first:

<?php
class BulletProofException extends Exception{}

namespace BulletProof;
/**
 * BulletProof ImageUploder:
...

File | Remove BOM in PhpStorm fixed this problem in both cases that I encountered it.


If you want to use this lib then in ImageUploader.php you should move BulletProofException definition after namespace declaration. Submit pull-request for this issue to lib repo :)

EDIT: Make some changes in head of file:

namespace {
    class BulletProofException extends Exception{}
}

namespace BulletProof {

    class ImageUploader
    { ... }
}

You must change the file encoding to ANSI. I was using Notepad++ with encoding UTF-8 and had the same problem. The problem disappeared after I change the file encoding to ANSI!


I fix it this way when I started doesn't matter utf8 just this way open <?php in the first line in the editor in my case sublime text and the namespace writte in the second line

2 <?php namespace mynamespace; //you should writte youe namespace down where you open php here should be in line 3 here I make the error cuz I started open from line 2 <?php

1 <?php namespace mynamespace; // I started from line 1 <?php it WORK


It is all about the namespace declaration. According to http://php.net/manual/en/language.namespaces.definition.php all namespace declaration must be put on the very top of the file after the <?php opening php tag. This means that before writing any code on the file, if that file needs to be contained in a namespace then you must first declare the namespace before writing any code. Like this:

<?php
namespace App\Suport\Facades;

class AuthUser {
  // methods and more codes
}

BUT the declare keyword can be put before the namespace declaration so this is still valid:

<?php
declare(maxTries = 3);

namespace App\Suport\Facades;

class AuthUser {
  // methods and more codes
}

all other stuff must be put AFTER the namespace keyword. For more info, read the http://php.net/manual/en/language.namespaces.definition.php.


Sometimes the space after <?php visible if you are using the same editor, in my case I was using TextWrapper. Due to the file type configuration in the TextWrapper (in your case some other editor), you might not see the space. Use a different editor in your machine and you'll see it. Then remove them -- works perfectly afterward.


Edit ImageUploader.php - either remove line (cause BulletProofException not used anywhere)

class BulletProofException extends Exception{}

or move it under line

namespace BulletProof;

Make sure there is no whitespace before your php tag

// whitespace
<?php
    namespace HelloWorld
?>

Remove the white space before your php tag starts

<?php
    namespace HelloWorld
?>

Namespace declarat 123456789101112

<?php
    namespace app\controllers;

    use yii\web\Controller;
    use app\models\users;
    class UserController extends Controller {
        public function actionIndex() {
            echo "working on .....";
        }    
    }

Sometimes this issue come because of space in PHP start tag of controller facing same issue just removed whitespace in:

<?php 
namespace App\Http\Controllers\Auth;

removing the space resolved my error


i also got the same problem by copying the controller code from another controller, then i solve the problem by removing the space before php tag

            <?php

In my case, the file was created with UTF-8-BOM encoding. I have to save it into UTF-8 encoding, then everything works fine.