php 致命错误:无法重新分配自动全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14954446/
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 08:16:24  来源:igfitidea点击:

Fatal error: Cannot re-assign auto-global variable

php

提问by sandeep

Fatal error: Cannot re-assign auto-global variable _FILES in C:\xampp\htdocs\user\utils\CommonUtils.php on line 1395

致命错误:无法在第 1395 行的 C:\xampp\htdocs\user\utils\CommonUtils.php 中重新分配自动全局变量 _FILES

The code on line 1395 is

第 1395 行的代码是

public static function saveAvatar($code, $pilotid, $_FILES) {

回答by Yogesh Suthar

you can't use $_FILESfor function parameter it's reserved word, use this instead of

您不能将其$_FILES用作保留字的函数参数,请使用 this 代替

public static function saveAvatar($code, $pilotid, $files) { }

and for calling pass the $_FILESlike this

$_FILES像这样调用传递

saveAvatar($code, $pilotid, $_FILES);

OR

或者

You can also directly access the $_FILESwithout passing it in function parameter inside function.

您也可以直接访问$_FILES而不在函数内部传递函数参数。

回答by Mark Baker

You're trying to set a variable called $_FILES in local scpe as an argument to the saveAvatar() method; but can't because it's one of the special superglobals.

您试图在本地 scpe 中设置一个名为 $_FILES 的变量作为 saveAvatar() 方法的参数;但不能,因为它是特殊的超全局变量之一。

Change the line to

将行更改为

public static function saveAvatar($code, $pilotid) {

The $_FILES superglobal will still be available to that method simply because it is a superglobal

$_FILES 超全局变量仍可用于该方法,因为它是超全局变量

回答by Hafiz Hassan Latif

I was also facing the same problem. Then I just removed the $_FILES variable from list of variables and my website started working again.

我也面临同样的问题。然后我从变量列表中删除了 $_FILES 变量,我的网站又开始工作了。

回答by Kannadasan K

Normally we can't reassign the $_Files, which means we can't pass the auto super global variable as argument -or function. But we have an alternate solutions.

通常我们不能重新分配 $_Files,这意味着我们不能将 auto 超级全局变量作为参数 - 或函数传递。但我们有一个替代解决方案。

Pass the file as parameter

将文件作为参数传递

  function ImageProcess(array $_File){
           $image        = $_FILES["file"]["name"];
           $uploadedfile = $_FILES['file']['tmp_name'];
           //Write your code here...
  }

Call the function with auto global-variable as argument.

使用自动全局变量作为参数调用函数。

  if ($_SERVER['REQUEST_METHOD'] == "POST") {
      if(isset($_FILES['file'])){
         echo ImageProcess($_FILES['file']);
      }
  }

Form for upload

上传表格

    <form method="post" action="<?php $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
       <input type="file" name="file" /> 
       <button type="submit">Update &amp; Save</button>
    </form>