我正在尝试使用下面的表单向MySQL数据库提交4个图像。当我点击"上传图像"按钮时,会出现一个错误,说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;
}
通常,该错误来自PHP解析器,在找到预期语法之前到达脚本末尾。很可能是未闭合的大括号}
。
在文件的最后再添加一个大括号}
。
您从未关闭过assertValidUpload()
函数。
通常应该避免编写如此冗长的函数。试着把它分解成多个函数--一般来说,一个函数应该只完成一个任务。
另外,你的代码风格不一致(可能只是这个网站上的格式(:
在assertValidUpload()
函数的开头,将大括号放在下一行,应该将其移到与函数定义声明相同的行。这类事情有助于您的代码中的不一致性脱颖而出,从而帮助您注意到这样的错误。