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

PhpImage Upload

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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
?>

Solution 3 - Php

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.

Solution 4 - Php

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

Solution 5 - Php

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!

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

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

Solution 11 - Php

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
    { ... }
}

Solution 12 - Php

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

class BulletProofException extends Exception{}

or move it under line

namespace BulletProof;

Solution 13 - Php

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.

Solution 14 - Php

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.

Solution 15 - Php

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!

Solution 16 - Php

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 .....";
        }	 
    }

Solution 17 - Php

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

Solution 18 - 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.

Solution 19 - Php

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.

Solution 20 - 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.

Solution 21 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2536244View Question on Stackoverflow
Solution 1 - PhpVivek ChaudhariView Answer on Stackoverflow
Solution 2 - PhpPriyankView Answer on Stackoverflow
Solution 3 - PhpSaidul HaqueView Answer on Stackoverflow
Solution 4 - PhpmystiscoolView Answer on Stackoverflow
Solution 5 - PhpPavel KenarovView Answer on Stackoverflow
Solution 6 - Phpuser2338925View Answer on Stackoverflow
Solution 7 - PhpDanila PiatovView Answer on Stackoverflow
Solution 8 - Phpuser2536244View Answer on Stackoverflow
Solution 9 - PhpAliView Answer on Stackoverflow
Solution 10 - Phpuser2672373View Answer on Stackoverflow
Solution 11 - PhpLeonov MikhailView Answer on Stackoverflow
Solution 12 - PhpS KorolevView Answer on Stackoverflow
Solution 13 - PhplenkoView Answer on Stackoverflow
Solution 14 - PhpaprilmintacpinedaView Answer on Stackoverflow
Solution 15 - PhpDavid A. GrayView Answer on Stackoverflow
Solution 16 - PhpMuhammad SaeedView Answer on Stackoverflow
Solution 17 - PhpBenjamin NoventaView Answer on Stackoverflow
Solution 18 - PhpClickBrightView Answer on Stackoverflow
Solution 19 - PhpMarkView Answer on Stackoverflow
Solution 20 - Phpvictor_luuView Answer on Stackoverflow
Solution 21 - PhpMarcusView Answer on Stackoverflow