php 致命错误:命名空间声明语句必须是脚本中的第一个语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21433086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:08:48  来源:igfitidea点击:

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

phpimage-upload

提问by Vivek Chaudhari

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 workingscript I would be appreciated)

我正在尝试使用它为我的网站创建图像表单上传,我使用它的原因是因为它比自己做所有事情更安全(但如果有人可以指出另一个工作脚本,我将不胜感激)

simon-eQ / ImageUploader

西蒙-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.

我试过包含,需要,但它仍然不起作用。

回答by Vivek Chaudhari

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

有时这个问题是因为控制器的 PHP 开始标签中的空间面临同样的问题,只是删除了以下空格:

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

removing the space resolved my error

删除空间解决了我的错误

回答by Priyank

Make sure there is no whitespacebefore your php tag

确保php 标签前没有空格

// whitespace
<?php
    namespace HelloWorld
?>

Remove the white space before your php tag starts

在 php 标签开始之前删除空格

<?php
    namespace HelloWorld
?>

回答by mystiscool

This threadseems 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?
  1. 您的虚拟主机是否可以在 PHP 代码之前将一些代码插入您的页面?
  2. 文档开头是否有 UTF-8 字节顺序标记?

On the other hand, it could also be a bug in ImageUploader... the main PHP fileputs 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:

另一方面,它也可能是 ImageUploader 中的一个错误......主要的 PHP 文件将命名空间放在类定义之后,我在 PHP 文档中没有看到,它说它应该是第一个 PHP 代码。从这个页面

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.

命名空间是使用命名空间关键字声明的。包含命名空间的文件必须在任何其他代码之前在文件顶部声明命名空间 - 有一个例外:declare 关键字。

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:

这里没有declare关键字,所以这可能是源代码中的一个bug,是开发者版本的PHP漏掉的,因为他没有把命名空间放在第一位:

<?php
class BulletProofException extends Exception{}

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

回答by Saidul Haque

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 文件中,我编写了以下代码,其中 php 开始标记之前有一些空格

<?php
namespace App\Controller;

when I remove that space, it solved.

当我删除那个空间时,它解决了。

回答by Saidul Haque

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

如果您使用 IDE,则必须从第一行开始您的代码。示例我使用 aptana studio3

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

Hope it helps.That solves my problem,.

希望它有帮助。这解决了我的问题,。

回答by Pavel Kenarov

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

您必须将文件编码更改为ANSI。我在使用 Notepad++ 编码UTF-8 时遇到了同样的问题。将文件编码更改为ANSI后,问题消失了!

回答by Danila Piatov

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

档案 | 在我遇到的两种情况下,PhpStorm 中的Remove BOM都修复了这个问题。

回答by Danila Piatov

The library isn't working at all, setSizeLimitis broken, setImageSizeis ignored as-well, just don't use this.

图书馆根本不工作,setSizeLimit坏了,setImageSize也被忽略了,只是不要使用它。

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

编辑:大小似乎以字节为单位,即使错误显示为千字节。

回答by Danila Piatov

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

可惜我不在 Github;否则可能会提出请求:(

回答by Leonov Mikhail

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

如果你想使用这个库,那么在 ImageUploader.php 你应该BulletProofException在命名空间声明之后移动定义。将此问题的拉取请求提交到 lib repo :)

EDIT: Make some changes in head of file:

编辑:在文件头中进行一些更改:

namespace {
    class BulletProofException extends Exception{}
}

namespace BulletProof {

    class ImageUploader
    { ... }
}