Fatal error: Namespace declaration statement has to be the very first statement in the script in
Asked Answered
S

21

65

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.

Sink answered 29/1, 2014 at 13:52 Comment(4)
That file is broken (the last commit broke it, probably was not intentional). Submit a bug report.Bianchi
The library isn't working at all, setSizeLimit is broken, setImageSize is ignored as-well, just don't use this.Sink
@Sink Please try it now. It is my lib. Plus check my answer and tell me for the moment if there is anything wrong. As jon said, the last commit broke it.Exegete
Change encoding to iso-8859-1Ilyssa
P
96

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

Pasteurize answered 6/7, 2016 at 9:0 Comment(4)
I had the same problem within my code <?php (followed by lots of space) namespace um\core; Removing the space resolved itDefy
Errors like this make me wonder what the programming must have been like in the early days, before stackoverflowJournal
Absolute legend. This just saved me. Those dastardly spaces!Frumpy
Also check that your IDE create UTF-8 files (with NO BOM) UTF-8 BOM. For example, in phpStorm Settings / Editor / File Encodings -> Create UTF-8 files: with NO BOMBrilliancy
E
87

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
?>
Eschew answered 27/8, 2018 at 6:3 Comment(0)
Z
18

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!

Zweig answered 24/11, 2015 at 8:54 Comment(4)
I guess that a byte order mark (BOM) might be the culprit. Some editors save UTF-8 documents with it by default and it then tends to break scripts. See en.wikipedia.org/wiki/Byte_order_markSkeie
Who would have guessed, +1Indictment
Choosing ANSI is really just a work-around of the BOM problem. I recommend to store your files using UTF-8 without the BOM to avoid charset problems down the line. If your editor cannot save without a BOM, it's time to switch your editor to a professional one, instead.Tericaterina
I was using PhpStorm ( with setting UTF-8 without the BOM ) and got this problem - always using PhpStorm as editor and don't think the problem comes from editor.Zweig
A
16

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.

Astronomical answered 17/2, 2016 at 8:31 Comment(1)
Look very carefully.. mine was above the <?php but was scrolled away so it was hard to tell!Adjectival
P
12

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:
...
Profitable answered 29/1, 2014 at 14:4 Comment(1)
Damn you, BOM! Gets me every time.Refection
P
12

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,.

Physics answered 14/8, 2014 at 2:51 Comment(0)
D
12

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

Doubledecker answered 24/4, 2018 at 19:23 Comment(1)
This was the problem I had. The BOM counts as whitespace before the PHP tag. With Vim, set nobomb | wq will remove the BOM.Parr
V
4

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
Vulcan answered 10/7, 2018 at 11:13 Comment(0)
S
3

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.

Sink answered 29/1, 2014 at 14:32 Comment(2)
The library works 100%. Just has some typos to fix. I added some codes I didn't keep tabs on. I was actually working on it last night to re-write it from the beginning with more features and already 50% is finished..Exegete
->setImageSize(array("height"=>200, "width"=>200)) isn't working, throws those notices on line 229Sink
B
2

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 :(

Breakwater answered 29/1, 2014 at 13:58 Comment(0)
C
2

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
    { ... }
}
Clearcut answered 29/1, 2014 at 13:58 Comment(2)
If I do that: Fatal error: Class 'BulletProof\Exception' not found inSink
ok, wrap BulletProofException in to name space: ` namespace { class BulletProofException extends Exception{} } namespace BulletProof { ...class.... } `Clearcut
G
2

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

class BulletProofException extends Exception{}

or move it under line

namespace BulletProof;
Gelatinoid answered 29/1, 2014 at 13:59 Comment(0)
I
2

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.

Isisiskenderun answered 29/1, 2014 at 14:1 Comment(2)
No. The namespace can not be removed, otherwise it won't workExegete
I don't say that @Alez. Only moves the namespace definition to the top of the file ;-)Isisiskenderun
N
2

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.

Nel answered 18/4, 2017 at 8:43 Comment(0)
A
2

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!

Amphimixis answered 3/12, 2017 at 23:44 Comment(0)
I
1

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 .....";
        }    
    }
Implement answered 27/8, 2015 at 12:8 Comment(1)
You might want to explain why your solution works and how.Kauri
A
1

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

Aborigine answered 30/1, 2017 at 15:51 Comment(0)
R
1

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.

Reta answered 29/8, 2018 at 9:52 Comment(0)
S
1

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.
Smallage answered 13/5, 2019 at 11:0 Comment(0)
O
0

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.

Odyl answered 12/8, 2018 at 8:21 Comment(0)
T
-1

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

Theosophy answered 7/3, 2019 at 10:57 Comment(1)
What do you mean by "no space anywhere"?Bibliopegy

© 2022 - 2024 — McMap. All rights reserved.