我正在尝试使用下面的表单将4个图像提交到MySQL数据库。当我点击“上传图片”按钮时,会出现错误syntax error, unexpected $end in ...postsubmit.php on line 235
。 235行没有任何内容;它只是postsubmit.php的最后一行。知道我为什么会收到这个错误吗?
提前致谢,
约翰
表格:
echo '<form method="post" action="postsubmit.php" enctype="multipart/form-data">
<input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">
<div class="imagetitle"><label for="image">Image 1:</label></div>
<div class="imagefield"><input type="file" name="image" /></div>
<div class="imagetitle2"><label for="image2">Image 2:</label></div>
<div class="imagefield2"><input type="file" name="image2" /></div>
<div class="imagetitle3"><label for="image3">Image 3:</label></div>
<div class="imagefield3"><input type="file" name="image3" /></div>
<div class="imagetitle4"><label for="image4">Image 4:</label></div>
<div class="imagefield4"><input type="file" name="image4" /></div>
<div class="submissionbutton"><input type="submit" value="Upload Image" /></div>
</form>';
关于postsubmit.php:
function assertValidUpload($code)
{
if ($code == UPLOAD_ERR_OK) {
return;
}
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$msg = 'Image is too large';
break;
case UPLOAD_ERR_PARTIAL:
$msg = 'Image was only partially uploaded';
break;
case UPLOAD_ERR_NO_FILE:
$msg = 'No image was uploaded';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$msg = 'Upload folder not found';
break;
case UPLOAD_ERR_CANT_WRITE:
$msg = 'Unable to write uploaded file';
break;
case UPLOAD_ERR_EXTENSION:
$msg = 'Upload failed due to extension';
break;
default:
$msg = 'Unknown error';
}
throw new Exception($msg);
}
$errors = array();
try {
if (!array_key_exists('image', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image = $_FILES['image'];
// ensure the file was successfully uploaded
assertValidUpload($image['error']);
if (!is_uploaded_file($image['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info = getImageSize($image['tmp_name']);
if (!$info) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex) {
$errors[] = $ex->getMessage();
}
if (count($errors) == 0) {
// no errors, so insert the image
$query = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image['name']),
mysql_real_escape_string($info['mime']),
$image['size'],
mysql_real_escape_string(
file_get_contents($image['tmp_name'])
)
);
mysql_query($query);
try {
if (!array_key_exists('image2', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image2 = $_FILES['image2'];
// ensure the file was successfully uploaded
assertValidUpload($image2['error']);
if (!is_uploaded_file($image2['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info2 = getImageSize($image2['tmp_name']);
if (!$info2) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex2) {
$errors2[] = $ex2->getMessage();
}
if (count($errors2) == 0) {
// no errors, so insert the image
$query2 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image2['name']),
mysql_real_escape_string($info2['mime']),
$image2['size'],
mysql_real_escape_string(
file_get_contents($image2['tmp_name'])
)
);
mysql_query($query2);
try {
if (!array_key_exists('image3', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image3 = $_FILES['image3'];
// ensure the file was successfully uploaded
assertValidUpload($image3['error']);
if (!is_uploaded_file($image3['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info3 = getImageSize($image3['tmp_name']);
if (!$info3) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex3) {
$errors3[] = $ex3->getMessage();
}
if (count($errors3) == 0) {
// no errors, so insert the image
$query3 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image3['name']),
mysql_real_escape_string($info3['mime']),
$image3['size'],
mysql_real_escape_string(
file_get_contents($image3['tmp_name'])
)
);
mysql_query($query3);
try {
if (!array_key_exists('image4', $_FILES)) {
throw new Exception('Image not found in uploaded data');
}
$image4 = $_FILES['image4'];
// ensure the file was successfully uploaded
assertValidUpload($image4['error']);
if (!is_uploaded_file($image4['tmp_name'])) {
throw new Exception('File is not an uploaded file');
}
$info4 = getImageSize($image4['tmp_name']);
if (!$info4) {
throw new Exception('File is not an image');
}
}
catch (Exception $ex4) {
$errors4[] = $ex4->getMessage();
}
if (count($errors4) == 0) {
// no errors, so insert the image
$query4 = sprintf(
"insert into images (filename, mime_type, file_size, file_data)
values ('%s', '%s', %d, '%s')",
mysql_real_escape_string($image4['name']),
mysql_real_escape_string($info4['mime']),
$image4['size'],
mysql_real_escape_string(
file_get_contents($image4['tmp_name'])
)
);
mysql_query($query4);
header("Location: http://www...com/.../");
exit;
}
答案 0 :(得分:2)
通常,该错误来自PHP解析器,在找到预期语法之前到达脚本末尾。很可能是一个未闭合的大括号}
。
答案 1 :(得分:1)
在文件的最后添加一个右括号}
。
您从未关闭assertValidUpload()
功能。
另外,您的代码样式不一致(可能只是此站点上的格式):
在assertValidUpload()
函数的开头,您将左括号放在下一行,您应该将它移动到与函数定义声明相同的行。这些事情有助于你的代码中的不一致性 - 从而帮助你发现这样的错误。