php - 在php文件中获取解析错误
问题描述
我在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
这个错误有什么解决办法吗?
解决方案
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;
}
}
}
我正在用手机接听,请原谅格式问题
推荐阅读
- mysql - mysql GROUP BY 显示所有对应字段
- c++ - 使用 libclang 6.0.1:找不到 stddef.h
- html - 如何在 HTML 上向 Youtube 视频添加自动播放和循环?
- polymer - 使用 lit.html 渲染对象
- android - 谷歌地图不会在发布模式下呈现
- r - 使用 R 中的 any() 实现检测数字是否为素数的函数
- java - 使用 Java 在 Watson 聊天机器人中添加多个响应
- java - Sharedpreferences 与切换按钮不保存
- python - 需要在 tcp 服务器中添加定时器线程
- azure - 多租户中的 Azure 管理员同意不起作用