在php文件中获取解析错误

时间:2018-06-29 17:48:40

标签: php parse-error

我用php写了一些代码。示例代码片段如下:

    include('config.php');
    require_once "variables.php";

    global $uploadID = " " ;    //getting error in this line

    function uploadImage($wtI,$tbln,$pri,$db){
        if(is_array($_FILES)) {
        if(is_uploaded_file($_FILES['image']['tmp_name'])) {
            $sourcePath = $_FILES['image']['tmp_name'];
            $targetFolder = "../upload_images/$wtI/";
            if (!file_exists($targetFolder)) {
                mkdir($targetFolder, 0777, true);
            }
            $targetPath = $targetFolder.$_FILES['image']['name'];
            while(file_exists($targetPath)){
                $targetPath = $targetFolder.uniqid().'-'.$_FILES['image']['name'];
            }
            if(move_uploaded_file($sourcePath,$targetPath)){

                $sql = "UPDATE `$tbln` SET image='".substr($targetPath,3)."' WHERE $pri=$uploadID;";
                $result=mysqli_query($db,$sql);
                return true;
            }
            else return false;
        }
    }
 }

问题是我在运行php文件时收到以下错误消息:

Parse error:syntax error, unexpected '=', expecting ',' or ';' in C:\wamp64\www\project\php\additem.php on line 6

此错误有解决方案吗?

1 个答案:

答案 0 :(得分:1)

global关键字使您可以访问全局变量,而不是创建新变量。只需在此处删除全局关键字即可。 全局关键字必须放在要使用该变量的函数中。 检查https://www.w3schools.com/php/php_variables.asp以了解其用法。

您的代码更正为:

include('config.php');
require_once "variables.php";
// Changes start here
$uploadID = " ";    //getting error in this line

function uploadImage($wtI,$tbln,$pri,$db){
    global $uploadID;
    //Changes end here
    if(is_array($_FILES)) {
    if(is_uploaded_file($_FILES['image']['tmp_name'])) {
        $sourcePath = $_FILES['image']['tmp_name'];
        $targetFolder = "../upload_images/$wtI/";
        if (!file_exists($targetFolder)) {
            mkdir($targetFolder, 0777, true);
        }
        $targetPath = $targetFolder.$_FILES['image']['name'];
        while(file_exists($targetPath)){
            $targetPath = $targetFolder.uniqid().'-'.$_FILES['image']['name'];
        }
        if(move_uploaded_file($sourcePath,$targetPath)){

            $sql = "UPDATE `$tbln` SET image='".substr($targetPath,3)."' WHERE $pri=$uploadID;";
            $result=mysqli_query($db,$sql);
            return true;
        }
        else return false;
    }
}

}

我正在用电话接听,请原谅格式问题